xcode - AVPlayer 一旦移动到背景就卡住

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



}

和应用委托(delegate)部分

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

}


}

最佳答案

这是一个简单的修复,您需要创建一个 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)

关于xcode - AVPlayer 一旦移动到背景就卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667226/

相关文章:

ios - 想要制作我可以在制作动画时按下的旋转按钮

swift - SKScene 中的应用程序购买不起作用?

swift - UIView 动画表现怪异

iOS - float 单按钮

swift2 - iOS CVPixelBufferCreate在swift 2中泄漏内存

objective-c - PresentViewController 会自动销毁以前的 View 吗?

iphone - Xcode 4.3 和在 iOS 3.x 设备上调试

Xcode 6 文档 : How to increase font size

带有自定义 C++ 库的 PHP 扩展

swift - UITableViewAutomaticDimension 的最小单元格高度