在AVFoundation中有一种相对简单的循环视频方式吗?
我已经创建了我的AVPlayer和AVPlayerLayer:
avPlayer = [[AVPlayer playerWithURL:videoUrl] retain];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
avPlayerLayer.frame = contentView.layer.bounds;
[contentView.layer addSublayer: avPlayerLayer];
Run Code Online (Sandbox Code Playgroud)
然后我播放我的视频:
[avPlayer play];
Run Code Online (Sandbox Code Playgroud)
视频播放正常但最后停止播放.使用MPMoviePlayerController,您只需将其repeatMode
属性设置为正确的值即可.AVPlayer似乎没有类似的属性.似乎没有一个回调可以告诉我什么时候电影结束所以我可以寻找开头并再次播放它.
我没有使用MPMoviePlayerController,因为它有一些严重的限制.我希望能够一次播放多个视频流.
我一直在尝试找出如何使用 循环播放多个视频AVPlayerLooper
,但它们templateItem
采用 type 参数AVPlayerItem
而不是[AVPlayerItem]
。我目前正在使用 anAVQueuePlayer
来显示视频,但我需要循环播放它。
到目前为止,这是我的代码:
class MyVC: UIViewController {
@IBOutlet weak var playerView: UIView!
lazy var backgroundVideoPlayer = AVQueuePlayer()
// View Controller related code (viewDidLoad, etc.) is taken out for brevity.
private func loadBackgroundVideosRandomly() -> [AVPlayerItem] {
let mainBundle = Bundle.main
let movieURLs = [mainBundle.url(forResource: "Boop Burj Al Arab", withExtension: "mov"),
mainBundle.url(forResource: "Boop Dubai", withExtension: "mov"),
mainBundle.url(forResource: "Boop Dubai Clock", withExtension: "mov"),
mainBundle.url(forResource: "Boop Dubai Lake", withExtension: "mov")].shuffled()
let …
Run Code Online (Sandbox Code Playgroud)