完成后,Swift AVPlayerItem关闭

时间:2016-01-27 21:51:01

标签: ios swift avplayer avplayeritem

我对Swift和iOS开发非常陌生,所以请原谅我的无知。

我正在尝试在视频播放完毕后自动关闭AVPlayer。我曾想过附加“playerDidFinishPlaying”监听器来接收通知,但是一旦我拥有它,我找不到关闭Controller的方法/事件。我想模仿点击“完成”按钮的动作。

这是一小段代码。希望这是足够的信息。如果没有,我可以提供更多信息

let destination = segue.destinationViewController as! AVPlayerViewController
let url = NSURL(string: "video url")
destination.player = AVPlayer(URL: url!)
destination.player?.play()

我已经添加了以下通知,但是再次,我不知道一旦我拥有它就该怎么办...

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
    name: AVPlayerItemDidPlayToEndTimeNotification, 
    object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    // close window/controller
}

最后,我知道我需要删除观察者,但我不确定何时或何地这样做。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

为了"关闭"控制器,你应该拨打dismissViewControllerAnimated(true, completion: nil)

所以代码看起来像:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
    name: AVPlayerItemDidPlayToEndTimeNotification, 
    object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    dismissViewControllerAnimated(true, completion: nil)
}

如果您的viewController位于UINavigationController堆叠内,您也可以这样做:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
        name: AVPlayerItemDidPlayToEndTimeNotification, 
        object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    navigationController?.popViewControllerAnimated(true)
}

要删除观察者,您可以在deinit{}内完成:

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

答案 1 :(得分:2)

#iOS 11和快速4.2的关闭控制器不起作用,因此只需在播放器设置代码中添加此键

if #available(iOS 11.0, *) {
     self.playerVC?.exitsFullScreenWhenPlaybackEnds = true
}

如果您也想允许#iOS 10,请写下这一行。

if #available(iOS 11.0, *) {
      self.playerVC?.exitsFullScreenWhenPlaybackEnds = true
 }

NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC?.player!.currentItem)

func playerItemDidReachEnd(note:NSNotification){
     print("finished")
     dismissViewControllerAnimated(true, completion: nil)
 }

答案 2 :(得分:0)

SWIFT 3更新:

注意:您正在播放音频需求的viewController:AVAudioPlayerDelegate

你也不需要观察者

class myViewController: UIViewController, AVAudioPlayerDelegate {

var audioplayer = AVAudioPlayer() 

override func viewDidAppear(_ animated: Bool) {

if soundsON{
        let myFilePathString = Bundle.main.path(forResource: "backgroundSound", ofType: "mp3")

        if let myFilePathString = myFilePathString
        {
            let myFilePathURL = URL(fileURLWithPath: myFilePathString)

            do{
                try audioplayer = AVAudioPlayer(contentsOf: myFilePathURL)
                audioplayer.delegate = self
                audioplayer.prepareToPlay()

                audioplayer.play()


            }catch{
                print("error playing coin sound")
            }
        }
   }    
}

在此示例中,声音将在viewDidAppear播放,完成后将调用:audioPlayerDidFinishPlaying:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    //Check if the sound that finishes comes from a certain AVAudioPlayer 
    if player == audioplayer{
        print("The sound from -audioplayer- has finished")
    // If you need to dismiss the VC:
        dismiss(animated: true, completion: nil)
    }
}