0

i have black screen after video is over, but i would like to be redirect to another storyboard when video is over.

Could somebody help me with this.

Thanks you

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        playVideo()
    }

    private func playVideo() {
        guard let path = Bundle.main.path(forResource: "Kristinka", ofType:"m4v") else {
            debugPrint("video.m4v not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()
        playerController.showsPlaybackControls = false

        playerController.player = player

        present(playerController, animated: true) {

            player.play()



            }
        }


}
Scriptable
  • 19,402
  • 5
  • 56
  • 72
lukaspro
  • 1
  • 1
  • Possible duplicate of [No AVPlayer Delegate? How to track when song finished playing? Objective C iPhone development](https://stackoverflow.com/questions/6837002/no-avplayer-delegate-how-to-track-when-song-finished-playing-objective-c-iphon) – Scriptable Feb 18 '19 at 14:55
  • ^^ Check the suggested duplicate, there is a Swift answer there – Scriptable Feb 18 '19 at 14:56

1 Answers1

0

You need to detect when the item you're playing has reached the end. To do that, you can add an observer. For example:

func finishedVideo(_ notification: NSNotification) {
    print("Animation did finish")
}

private func playVideo() {
    guard let path = Bundle.main.path(forResource: "Kristinka", ofType:"m4v") else {
        debugPrint("video.m4v not found")
        return
    }
    let player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerController = AVPlayerViewController()
    playerController.showsPlaybackControls = false

    playerController.player = player

    present(playerController, animated: true) {

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(finishedVideo(_:)),
                                               name: .AVPlayerItemDidPlayToEndTime,
                                               object: player?.currentItem)

        player.play()



    }
}

And I dug this code up on this very related question. There's a lot of related/duplicate answers if you do a search.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215