AVPlayer冻结一旦移到后台

Ope*_*pei 4 xcode swift xcode7 swift2 tvos

我有以下代码加载视频并循环它.但是,当按下主页并返回应用程序时,视频会冻结,不会再次播放.我觉得答案是在AppDelegate函数中,但似乎无法让它们工作.

import UIKit
import AVKit
import AVFoundation



class ViewController: UIViewController, UIApplicationDelegate {



var videoPlayer: AVPlayer!
var playerLayer: AVPlayerLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
    let url = NSURL(fileURLWithPath: path!)
    let playerItem = AVPlayerItem(URL: url)

    self.videoPlayer = AVPlayer(playerItem: playerItem)
    self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
    self.playerLayer!.frame = self.view.frame
    self.videoPlayer!.play()

    self.view.layer.addSublayer(self.playerLayer!)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidReachEnd:"), name: AVPlayerItemDidPlayToEndTimeNotification, object:nil)
}


func playerDidReachEnd(notification: NSNotification) {
    self.videoPlayer.seekToTime(kCMTimeZero)
    self.videoPlayer.play()
}


func applicationWillResignActive(application: UIApplication) {
    videoPlayer.pause()
}


func applicationDidBecomeActive(application: UIApplication) {
    videoPlayer.play()
}


func applicationDidEnterBackground(application: UIApplication) {
    videoPlayer.pause()
}


func applicationWillEnterForeground(application: UIApplication) {
    videoPlayer.play()
}





override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



}
Run Code Online (Sandbox Code Playgroud)

这是我用来解决它的问题.

import UIKit
import AVKit
import AVFoundation



class ViewController: UIViewController, UIApplicationDelegate {



var videoPlayer: AVPlayer!
var playerLayer: AVPlayerLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
    let url = NSURL(fileURLWithPath: path!)
    let playerItem = AVPlayerItem(URL: url)

    self.videoPlayer = AVPlayer(playerItem: playerItem)
    self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
    self.playerLayer!.frame = self.view.frame
    self.videoPlayer!.play()

    self.view.layer.addSublayer(self.playerLayer!)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("continueVideo:"), name: "continueVideo", object: nil)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidReachEnd:"), name: AVPlayerItemDidPlayToEndTimeNotification, object:nil)
}


func playerDidReachEnd(notification: NSNotification) {
    self.videoPlayer.seekToTime(kCMTimeZero)
    self.videoPlayer.play()
}

func continueVideo(notification: NSNotification) {
    self.videoPlayer.play()
}

func applicationWillResignActive(application: UIApplication) {
    videoPlayer.pause()
}


func applicationDidBecomeActive(application: UIApplication) {
    videoPlayer.play()
}


func applicationDidEnterBackground(application: UIApplication) {
    videoPlayer.pause()
}


func applicationWillEnterForeground(application: UIApplication) {
    videoPlayer.play()
}





override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



}
Run Code Online (Sandbox Code Playgroud)

和app委托部分

import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    return true
}

func applicationWillResignActive(application: UIApplication) {


}

func applicationDidEnterBackground(application: UIApplication) {


}

func applicationWillEnterForeground(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName ("continueVideo", object:nil)
}

func applicationDidBecomeActive(application: UIApplication) {

}

func applicationWillTerminate(application: UIApplication) {

}


}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ppz 6

这是一个简单的修复,你需要创建一个NSNotification并从你的AppDelegate:

func applicationWillEnterForeground(application: UIApplication) {
     NSNotificationCenter.defaultCenter().postNotificationName("continueVideo", object: nil)
}
Run Code Online (Sandbox Code Playgroud)

SWIFT3更新

func applicationWillEnterForeground(_ application: UIApplication) {
    // NOTE: Notification.Name raw value should be global 
    NotificationCenter.default.post(name: Notification.Name(rawValue: "com.yourappname.continueVideo"), object:self)
}
Run Code Online (Sandbox Code Playgroud)

并以正常方式接收通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "<funcName>:", name: "continueVideo", object: nil)
Run Code Online (Sandbox Code Playgroud)

SWIFT3更新

NotificationCenter.default.addObserver(self, selector: #selector(<funcName>), name: Notification.Name(rawValue: "com.yourappname.continueVideo"), object: nil)
Run Code Online (Sandbox Code Playgroud)