Swift AVPlayerItem closes after completion

I am extremely new to Swift and iOS, so please forgive my ignorance.

I am trying to automatically close AVPlayer when the video is playing. I decided to attach the "playerDidFinishPlaying" listener to receive the notification, but as soon as I find it, I cannot find the method / event to close the controller. I want to emulate the action of clicking Finish.

Here is a small piece of code. I hope this is enough. If not, I can provide additional information.

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

I added the following notice, but again, I'm not sure what to do with it when I have it ...

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

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

Finally, I know that I need to remove the observer, but I'm not sure when and where to do it. Any help is appreciated.

+1
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)
}
+2

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

Source: https://habr.com/ru/post/1659722/


All Articles