2

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

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

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

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
}

最后,我知道我需要移除观察者,但我不确定何时何地这样做。任何帮助是极大的赞赏。

4

3 回答 3

4

为了“关闭”控制器,您应该调用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)
}

如果您viewControllerUINavigationController堆栈内,您还可以执行以下操作:

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)
}
于 2016-01-28T16:50:55.500 回答
2

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

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)
 }
于 2018-10-05T11:21:13.220 回答
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)
    }
}
于 2016-10-14T16:30:59.870 回答