我在手机视图AVPlayer上有一段视频,但如果按下主页键,应用程序就会折叠-视频会自动停止,如果再次打开应用程序-应用程序就会暂停。使用AppDelegate applicationWillEnterForeground时,我需要查看视图并尝试继续播放视频,但它继续被暂停。在其他视图中转换并返回后,视频将继续播放。你能建议一下如何在返回应用后播放视频吗?
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
public func test()
{
print("1111")
if player?.timeControlStatus == .playing
{
print("playing") // not print
}
else if player?.timeControlStatus == .paused
{
player?.play()
print("paused") // not print
}
}
private func playVideo()
{
let file1 : String = "portable.mp4";
let file = file1.components(separatedBy: ".")
guard let path = Bundle.main.path(forResource: file[0], ofType:file[1]) else {
debugPrint( "\(file.joined(separator: ".")) not found")
return
}
player = nil
player = AVPlayer(url: URL(fileURLWithPath: path))
player?.actionAtItemEnd = .none
player?.isMuted = false
player?.volume = 0.2
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.zPosition = -1
playerLayer.frame = view.frame
view.layer.addSublayer(playerLayer)
player?.play()
print("222")
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { _ in
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
}
}AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.enableAutoToolbar = false
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
print("000")
let ll: ViewController = ViewController()
ll.test()
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}发布于 2018-07-23 17:10:27
您可以在ViewController中使用UIApplicationDidBecomeActive UIApplicationWillResignActive,如下所示-
NotificationCenter.default.addObserver(self, selector: #selector(appBecomeActive), name: .UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appResignActive), name: .UIApplicationWillResignActive, object: nil)
@objc func appBecomeActive() {
player.play()//Play video
}
@objc func appResignActive() {
player.pause()//pause video
}https://developer.apple.com/documentation/uikit/uiapplication/1622953-didbecomeactivenotification
发布于 2018-07-23 17:11:11
您可以在AppDelgate方法中创建新的实例,但是这不是实现它的好方法。
在你的ViewController类中。您可以添加将通知您观察者,您可以执行任何操作
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: .main) {[weak self] (noti) in
guard let strongSelf = self else {return }
// DO here whatever you want
} 它会在你的app激活时被调用。
https://stackoverflow.com/questions/51475004
复制相似问题