-1

我有一个通过编码而不是故事板制作的视图控制器来显示视频。视频播放完毕后,我无法让视图控制器返回上一个。我有一个将用户带到视频的按钮,但是当视频完成时,它不会返回到上一个视图控制器。

import AVKit
import AVFoundation
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


    }

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

        let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "tour", ofType: "mov")!))


   let vc = AVPlayerViewController()
      vc.player = player
     present(vc, animated: true)
    }


}

这是我的视频代码。我已经完成了在我的故事板上构建应用程序的所有其他工作

4

1 回答 1

0

要在视频结束后返回上一个 VC,您需要 -

  1. 添加一个观察者,它可以检测到视频何时停止播放,然后 -
  2. 解雇AVPlayerViewController

完成任务 1 - Channel 的这个答案有助于设置观察者 - https://stackoverflow.com/a/40056529/13451699

AVPlayerViewController要执行任务 2 - 只需从函数内部解除playerDidFinishPlaying,如下所示:

func playerDidFinishPlaying(note: NSNotification){
  // Video finished playing
  self.dismiss(animated: true, completion: nil)
}
于 2020-05-04T04:35:23.693 回答