菜鸟教程小白 发表于 2022-12-13 14:53:29

ios - 使用 AVPlayer-Swift 无缝循环播放视频


                                            <p><p>我正在使用 AVPlayer 在后台使用循环播放本地视频,视频播放正常,但在完成视频后需要暂停才能循环播放视频。我尝试了很多方法,也看到了很多关于堆栈溢出的帖子,但我没有找到合适的解决方案。我正在使用 Swift3。</p>

<p>代码在这里:</p>

<pre><code>var videoplayer :AVPlayer = AVPlayer()

override func viewDidLoad() {
super.viewDidLoad()

let path = Bundle.main.path(forResource: &#34;background4&#34;, ofType: &#34;mp4&#34;)
      videoplayer = AVPlayer(url: URL(fileURLWithPath: path!))
      videoplayer.volume = 0   
      videoplayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none;

      let playerLayer = AVPlayerLayer(player: videoplayer)
      playerLayer.frame = self.view.frame
      playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

      if (videoplayer.rate != 0) {
            print(&#34;playing videoplayer&#34;)
            self.blurBgImage.isHidden = true
      }

      playerLayer.zPosition = -1
      videoplayer.rate = 0
      videoplayer.play()
      self.blurBgImage.layer.addSublayer(playerLayer)

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: videoplayer.currentItem, queue: nil, using: { (_) in
      DispatchQueue.main.async {

             let t1 = CMTimeMake(5, 100)
            self.videoplayer.seek(to: t1)
            self.videoplayer.play()
      }
    })
}
</code></pre>

<p>我也尝试过 AVPlayerLooper。
代码是:</p>

<pre><code> var playerLooper: NSObject?
var playerLayer:AVPlayerLayer!
var queuePlayer: AVQueuePlayer?

func playVideo(_ filmName: String){
       if let path = Bundle.main.path(forResource: filmName, ofType: &#34;mp4&#34;)      let url =URL(fileURLWithPath: path)
         if #available(tvOS 10.0, *) {
          let playerItem = AVPlayerItem(url: url as URL)
            self.videoplayer = AVQueuePlayer(items: )
            self.playerLayer = AVPlayerLayer(player: self.videoplayer)
            self.playerLooper = AVPlayerLooper(player: self.videoplayer as! AVQueuePlayer, templateItem: playerItem)
            self.blurBgImage.layer.addSublayer(playerLayer!)
            self.playerLayer?.frame = self.view.frame
            self.videoplayer.volume = 10
            self.videoplayer.play()
      } else {
            videoplayer = AVPlayer(url: url)
            videoplayer.play()
            loopVideo(videoplayer)
      }
}
}
</code></pre>

<p>我应该怎么做才能无缝循环?提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>仅供引用,这里有示例代码:<a href="https://developer.apple.com/library/content/samplecode/avloopplayer/Introduction/Intro.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/content/samplecode/avloopplayer/Introduction/Intro.html</a> </p>

<hr/>

<p>@matt 删除的答案对我(在设备/模拟器上)适用于 iOS 10+ 设备:</p>

<blockquote>
<p>Use AVPlayerLooper. That is exactly what it is for.</p>

<p><a href="https://developer.apple.com/reference/avfoundation/avplayerlooper" rel="noreferrer noopener nofollow">https://developer.apple.com/reference/avfoundation/avplayerlooper</a></p>

<p>Basically it implements AVQueuePlayer for you, constantly updating the
queue so that it never ends.</p>
</blockquote>

<p>它无缝循环,没有任何白/黑光点。</p>

<p>例如</p>

<pre><code>private var looper: AVPlayerLooper?

...

let queuePlayer = AVQueuePlayer(playerItem: item)
looper = AVPlayerLooper(player: queuePlayer, templateItem: item)
videoPlayerLayer.player = queuePlayer
</code></pre>

<p>如果您最终在可重复使用的单元格(例如 <code>UICollectionView</code>)中执行此操作,请确保在重复使用单元格之前禁用循环,否则您会遇到一些难以理解的崩溃:</p>

<pre><code>looper?.disableLooping()
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 AVPlayer-Swift 无缝循环播放视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42105431/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42105431/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 AVPlayer-Swift 无缝循环播放视频