AVPlayer,视频结束后黑屏

问题描述 投票:0回答:1

视频结束后,我有黑屏,但我想在视频结束时重定向到另一个故事板。

有人可以帮我这个吗?

谢谢

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



            }
        }


}
swift video avplayer
1个回答
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上挖了这个代码。如果你进行搜索,会有很多相关/重复的答案。

© www.soinside.com 2019 - 2024. All rights reserved.