简体   繁体   English

如何停止AVQueuePlayer内存泄漏?

[英]How to stop AVQueuePlayer memory leak?

I'm using Xcode 7, Swift and iOS 9.0. 我正在使用Xcode 7,Swift和iOS 9.0。

If I... 如果我...

  • Create AVQueuePlayer with some items 使用某些项目创建AVQueuePlayer
  • Start to play it 开始播放
  • Then removeAllItems() ... the memory doesn't get released. 然后removeAllItems() ...不会释放内存。

If call this function, it starts to hog up memory: 如果调用此函数,它将开始占用内存:

var queuePlayer: AVQueuePlayer!

func startAgain(){

    if queuePlayer != nil{
        queuePlayer.pause()
        queuePlayer.removeAllItems()
        queuePlayer = nil
    }

    var items: [AVPlayerItem] = []

    for _ in 1 ... 10 {
        items.append(AVPlayerItem(URL: NSBundle.mainBundle().URLForResource("Music", withExtension: "mp3")!))
    }

    queuePlayer = AVQueuePlayer(items: items)
    queuePlayer.play()

    NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "startAgain", userInfo: nil, repeats: false)
}

I even subclassed AVPlayerItem and AVURLAsset to see if they get deinitialized... And they do! 我什至将AVPlayerItemAVURLAsset子类 ,看它们是否被初始化了……而且它们确实可以! So I have no idea why this is happening. 所以我不知道为什么会这样。

Using Instruments I can see that VM: Performance tool data is what is using the memory and it never gets released. 使用仪器我可以看到VM:性能工具数据正在使用内存,并且永远不会释放。

Do you have any ideas how to free up the memory? 您有什么想法释放内存吗?

What should I do with AVQueuePlayer in order it to release the memory? 我应该如何使用AVQueuePlayer释放内存?

You need to take care of releasing the current array var items: [AVPlayerItem]. 您需要注意释放当前的数组变量项:[AVPlayerItem]。 It would be better to create it as a class property. 最好将其创建为类属性。 From your code, the timer is not invalidated and therefore it keeps the old array. 根据您的代码,计时器不会失效,因此它将保留旧数组。 The best practice is to release the old timer before creating a new timer. 最佳做法是在创建新计时器之前释放旧计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM