【问题标题】:Terminated due to Memory Pressure when using iOS AVPlayer使用 iOS AVPlayer 时因内存压力而终止
【发布时间】:2014-09-21 18:24:40
【问题描述】:

在我的 iOS 应用程序中,我在 iOS YouTube helper library 的帮助下将 YouTube 视频作为循环运行。但我没有机会完整播放视频,但 20 秒后,我再次将同一视频排入队列,如下所示。

- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{

    if (state == kYTPlayerStateQueued) {
        startedTimer = NO;

        [self.playerView playVideo];      
    } else if (state == kYTPlayerStatePlaying) {

        if (!startedTimer) {
            startedTimer = YES;

            vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
        }
    } 
}

- (void)restartVideo {
    [self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}

这完全符合我的要求。

接下来我想在 YouTube 每次重播视频之前播放 mp4 文件。为了实现这一点,我使用了AVPlayer。然后代码发生了如下变化。

- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{

    if (state == kYTPlayerStatePlaying) {
        if (self.avPlayer != nil) {
            avPlayerLayer.hidden = YES;
            self.avPlayer = nil;
        }

        if (!startedTimer) {
            startedTimer = YES;

            vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
        }
    } 
}

- (void)restartVideo {
    self.avPlayer = [AVPlayer playerWithURL:introVideoFileURL];

    avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
    self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

    avPlayerLayer.frame = CGRectMake(0, 0, 320, 330);
    [self.view.layer addSublayer: avPlayerLayer];

    [self.avPlayer play];

    [self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}

在进行上述更改后,应用程序按预期运行,但运行近四分钟后,我的 Xcode 中出现“因内存压力而终止”弹出窗口,应用程序崩溃。我使用 Instruments 开发工具检查了内存,应用程序在崩溃时使用了近 35 MB 的内存。当应用程序开始运行时,它使用了超过 45 MB 的内存并且运行平稳。

如您所见,我仅在需要时创建 AVPlayer,并在完成工作后将其设置为零。这个问题可能是什么原因造成的,我该如何解决?

【问题讨论】:

  • 不完全是。我改变了播放器类型。现在使用MPMoviePlayerController 而不是AVPlayer
  • @BhaveshLathigara 你找到解决方案了吗?
  • 其实我正在为我的应用程序使用 KXMovie 播放器,由于内存压力,它会在 10 到 15 分钟后崩溃(收到内存警告)。但我现在没有解决办法。
  • iOS developer forum 中发布了该问题,并更改了 MPMoviePlayerController。
  • 我找到了我的代码的解决方案,即当内存压力我们的 -(void)didReceiveMemoryWarning 调用时,我将所有影响播放音频的对象清零并在需要时重新创建对象,所以现在它工作正常对我来说

标签: ios objective-c avplayer ytplayerview memory-pressure


【解决方案1】:

comments 中 Bhavesh Lathigara 的解决方案移至社区 Wiki 答案:

didReceiveMemoryWarning被调用时,nil所有与视频/音频播放相关的对象,并在需要时重新创建它们。

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多