2016-05-30 104 views
0

首先,感謝您的幫助。iOS AVPlayer播放大量視頻並導致內存過高

的問題:

1.I需要測試大量videos.I寫了兩個方法來測試視頻。

2.當播放視頻狀態發生變化時,我設置avPlayer = nil和avPlayerItem = nil,我也添加@autoreleasepool {},但內存緩慢增加,我使用了這些工具,但沒有找到內存泄漏,我發現很多問題並沒有找到解決方案。

3.Here是我的代碼:

-(void)CheckVideoURlCanPlay{ 
@autoreleasepool { 
    VideoPlayDataObj *videoPlay = [self.needCheckArr objectAtIndex:0]; 
    NSString *str = videoPlay.videoURL; 
    NSURL *url = [NSURL URLWithString:str]; 
    self.playItem = [AVPlayerItem playerItemWithURL:url]; 
    self.player = [AVPlayer playerWithPlayerItem:self.playItem]; 
    self.playLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
    self.playLayer.videoGravity = AVLayerVideoGravityResizeAspect; 
    self.playLayer.frame=CGRectMake(100, 80, 100, 100); 
    [self.view.layer addSublayer:self.playLayer]; 
    [self.player play]; 
    [self.playItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil]; 
    }} 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ 
@autoreleasepool { 

    if ([keyPath isEqualToString:@"status"]) { 
     if (self.player.currentItem.status == AVPlayerItemStatusReadyToPlay) { 
      @autoreleasepool { 

       [self.player.currentItem.asset cancelLoading]; 
       [self.player.currentItem cancelPendingSeeks]; 
       [self.player cancelPendingPrerolls]; 
       [self.playItem removeObserver:self forKeyPath:@"status"]; 
       self.playItem = nil; 
       self.player = nil; 

      } 
      [self.needCheckArr removeObjectAtIndex:0]; 
      [NSThread sleepForTimeInterval:5]; 
      [self CheckVideoURlCanPlay]; 
     } 
    } 
} 

}

4,我也試圖緩解當前視圖,但它並不能免除所有。如果你播放大量視頻內存會很高,最終的應用程序將被殺死。

5.我不知道加載視頻後是否會產生髒內存?

這是我的應用程序啓動內存:13M 本次測試的一些視頻和我彈出的看法:再次 enter image description here 本次測試的一些視頻和流行的觀點: enter image description here

6.Finally,我希望你能幫助我,非常感謝你。

回答

1

它看起來並不像你曾經從視圖中刪除的AVPlayerLayer:

[self.playerLayer removeFromSuperlayer]; 

我也看不到@autoreleasepool點這裏,特別是如果你有ARC打開(你幾乎肯定做! )。 @autoreleasepool通常只在您關閉ARC或在單個主循環調用中攪動大量內存(如,以兆字節爲單位)並且需要控制何時清理時纔有用。

+0

是的,這是我的錯,但問題不在這裏,我嘗試添加到它,但它不起作用。 –

+0

你是什麼意思「它不工作」?它崩潰? – damian

+0

是的,我發現這個代碼導致了這個問題。 self.player = [AVPlayer playerWithPlayerItem:self.playItem];我不知道如何解決。 –