Reuse same AVPlayer and AVPlayerLayer or create new ones each time?

If you need to play multiple videos in the same view, there are seemingly two approaches: (1) create new AVPlayerLayers and AVPlayers each time; or (2) reuse the same ones.

Assume your class is designed to play multiple videos, and the following code is used to load a new video:

    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)

The second option, after creating a AVPlayerLayer and AVPlayer, is to reuse them. So you could replace the last two lines with this:

    player.replaceCurrentItemWithPlayerItem(playerItem!)

What are the pros/cons of each option? Is it okay to use option 1, or is that considered wasteful?

链接地址: http://www.djcxy.com/p/56890.html

上一篇: 通过AVAsset与AVPlayerItem管理资产加载的优点?

下一篇: 重复使用相同的AVPlayer和AVPlayerLayer或每次创建新的?