AVPlayer,视频结束后黑屏

时间:2019-02-18 14:28:05

标签: swift video avplayer

视频结束后我的屏幕黑屏,但是我希望在视频结束后重定向到另一个故事板。

有人可以帮我吗?

谢谢

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



            }
        }


}

1 个答案:

答案 0 :(得分:0)

您需要检测正在播放的项目何时结束。为此,您可以添加一个观察者。例如:

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



    }
}

然后我在this very related question上挖了这段代码。如果您进行搜索,那么会有很多相关/重复的答案。