首页 文章

Swift AVPlayerItem完成后关闭

提问于
浏览
1

我是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

    为了"close"控制器,你应该调用 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)
    }
    
  • 2

    for #iOS 11 和swift 4.2 dismiss控制器不起作用所以只需在你的播放器设置代码中添加此键

    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

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

相关问题