如果你需要在同一个视图中播放多个视频,似乎有两种方法:(1)每次创建新的AVPlayerLayers和AVPlayers;或(2)重复使用相同的.
假设您的课程设计为播放多个视频,以下代码用于加载新视频:
var player = AVPlayer() var playerItem:AVPlayerItem? var playerLayer:AVPlayerLayer? // Get file path to <videoURL> videoURL = getFilePath(videoURL) // Create player & layer playerItem = AVPlayerItem(URL: NSURL(fileURLWithPath: videoURL)) player = AVPlayer(playerItem: playerItem!) playerLayer = AVPlayerLayer(player: player)
在创建AVPlayerLayer和AVPlayer之后,第二个选项是重用它们.所以你可以用这个替换最后两行:
player.replaceCurrentItemWithPlayerItem(playerItem!)
每个选项的优缺点是什么?是否可以使用选项1,还是被认为是浪费?