gpt4 book ai didi

ios - 在 View 中使用 AVPlayer 播放多个视频

转载 作者:行者123 更新时间:2023-11-28 10:11:39 25 4
gpt4 key购买 nike

我有一个 UIView,其中包含一些文本和 View 。在每个 View 中,我都需要使用 AVPlayer 播放视频,但我的问题是只有第一个 View 显示视频:

enter image description here

这是我的代码:

func playVideo(name:String , onView:UIView) {

let path = Bundle.main.path(forResource: name, ofType: "mp4")
let videoURL = URL(fileURLWithPath: path!)
let player = AVPlayer(url: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = onView.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
onView.layer.addSublayer(playerLayer)
player.play()

}

使用函数:

override func viewDidLoad() {
super.viewDidLoad()

//ScrollView
scrollView.contentSize = menuView.frame.size
scrollView.addSubview(menuView)

//Play videos
playVideo(name: "pizza", onView: videoView1)
playVideo(name: "salad", onView: videoView2)
playVideo(name: "fries", onView: videoView3)
}

当我运行应用程序时,视频仅在 videoView1 中播放,请问为什么会发生这种情况? 我还将我的代码放在 viewWillAppear , viewDidLayoutSubviews

最佳答案

根据 Apple 文档:

AVPlayer 类

Note

AVPlayer is intended for playing a single media asset at a time.

关于ios - 在 View 中使用 AVPlayer 播放多个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47246981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com