2016-01-27 109 views
1

我对Swift和iOS开发非常陌生,所以请原谅我的无知。Swift AVPlayerItem完成后关闭

我试图让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 
} 

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

+3

你的意思是你不能做'dismissViewControllerAnimated(真,完成:无)'里面你'playerDidFinishPlaying'?对于删除观察者,你总是可以放在'deinit {}' – Breek

+0

很确定这正是我要找的。谢谢! – twsmale

+1

我会发布答案,你可以标记这个问题为答案:) – Breek

回答

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

如果您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) 
} 
+0

太棒了。正是我在找什么。谢谢! – twsmale

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