Swift / Firebase-播放前先下载mp4

时间:2018-10-31 21:35:14

标签: swift firebase url avplayer

我希望播放mp4文件(存储在firebase存储器中),并在开始播放时启动计时器。 我的问题是计时器在视频显示在我的屏幕上之前启动。任何人都知道如何在视频播放之后开始时间?

这是我的代码,谢谢!

我的变种

var avPlayer:AVPlayer!
    var player:AVPlayer?
    var moviePalyer: AVPlayerLayer?
    var isdone = true
    var previewlayer: AVCaptureVideoPreviewLayer?
    var timer = Timer()

我的代码:

 let url = URL(string:movieURL)
let avPlayer = AVPlayer.init(url: url!)
moviePalyer = AVPlayerLayer.init(player: avPlayer)
moviePalyer?.frame = imageView.bounds
moviePalyer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
 imageView.layer.addSublayer(moviePalyer!)

avPlayer.play()
   self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyViewController.decreaseTimer), userInfo: nil, repeats: true)
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: nil, using: { (_) in
                                DispatchQueue.main.async {
                                    if self.isdone == true{
                                        avPlayer.seek(to: kCMTimeZero)
                                        avPlayer.play()

                                    }else {
                                        avPlayer.pause()
                                        self.isdone = false
                                    }
                                }
                            })

1 个答案:

答案 0 :(得分:0)

尝试一下:

    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: nil, using: { (_) in
                                DispatchQueue.main.async {
                                    if self.isdone == true{
                                        avPlayer.seek(to: kCMTimeZero)
                                        avPlayer.play()
                                 }else {
                                    avPlayer.pause()

                             self.isdone = false
                                }


                          }
                   DispatchQueue.main.async {
                         self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyViewController.decreaseTimer), userInfo: nil, repeats: true )
                        }

                        })