AVPlayer冻结一旦移到后台

时间:2015-11-12 08:34:46

标签: xcode swift swift2 xcode7 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()
    }



}

以下是我用来解决它的问题。

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()
    }



}

应用委托部分

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) {

}


}

1 个答案:

答案 0 :(得分:6)

这是一个简单的修复,您需要创建一个NSNotification并从AppDelegate调用它:

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

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)
}

并以正常方式接收通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "<funcName>:", name: "continueVideo", object: nil)

SWIFT3更新

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