【问题标题】:AVPlayer Freezes once moved to backgroundAVPlayer 一旦移动到后台就会冻结
【发布时间】:2015-11-12 08:34:45
【问题描述】:

我有以下代码可以加载视频并循环播放。但是,当按下 home 键并返回应用程序时,视频会冻结并且不会再次播放。我觉得答案在 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) {

}


}

【问题讨论】:

    标签: xcode swift swift2 xcode7 tvos


    【解决方案1】:

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

    【讨论】:

    • 不太确定如何接收通知?那会在视图控制器中吗? swift中的appdelegate部分也是?
    • 你已经在你的ViewDidLoad 中添加了一个Notification 已经添加了这个监听器,确保你创建了一个函数并改变了选择器(突出显示的
    • 您将在主文件夹中有一个标准的 AppDelegate.swift 文件。 func applicationWillEnterForeground(application: UIApplication) 应该已经在文件中了。
    • 是的,但是当我输入您在上面输入的内容时,它一直要求分隔符。它应该更像 NSNotificationCenter.defaultCenter().postNotificationName ("continueVideo", object:nil)
    • 是的,我明白了!非常感谢!我会为其他人更新我上面的代码。
    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2016-02-05
    相关资源
    最近更新 更多