【问题标题】:AVPlayer buffering video in background viewcontroller causes memory issuesAVPlayer 在后台视图控制器中缓冲视频会导致内存问题
【发布时间】:2017-01-11 19:55:04
【问题描述】:

我正在制作一个带有 AVPlayer 的视图控制器的直播电视流媒体应用。

- (void)viewDidLoad {
    self.playerViewController = [[AVPlayerViewController alloc] init];
    self.playerViewController.player = [AVPlayer playerWithURL:[NSURL URLWithString:StreamURL]];
    [self addChildViewController:self.playerViewController];
    [self.view addSubview:self.playerViewController.view];
    [self.playerViewController.player play];
}

我还有一个按钮,它显示一个带有附加信息的新视图控制器。当我展示新的视图控制器时,AVPlayer 会继续播放音频,这就是我想要的。

问题是 - 当我展示新的视图控制器时,AVPlayer 在后台播放音频但继续缓冲视频,当视图控制器被关闭时,AVPlayer 快​​速转发视频,以便它可以与音频同步.快速转发会导致内存使用量大幅增加,并且我收到内存不足警告。新视图控制器在前台的时间越长,当我关闭它时,内存跳跃就越大。

如何阻止AVPlayer 缓冲视频?

【问题讨论】:

    标签: ios objective-c avplayer


    【解决方案1】:

    我可能会迟到一点,但如果有人对此仍有疑问,我可以建议减少进入后台时的播放器缓冲区。我知道这不是一个完美的答案,但这仍然会有所帮助。

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onApplicationDidEnterBackgroundNotification:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onApplicationWillEnterForegroundNotification:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    

    然后

    - (void)onApplicationDidEnterBackgroundNotification:(NSNotification *)notification {
        self.playerItem.preferredForwardBufferDuration = 1;
    }
    - (void)onApplicationWillEnterForegroundNotification:(NSNotification *)notification {
        self.playerItem.preferredForwardBufferDuration = 0;
    }
    

    也别忘了在 dealloc 中移除观察者

     [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    【讨论】:

    • 这是一个很好的解决方法。当 AVPlayer 播放实时内容时,它在后台缓冲的主要原因是保持缓冲区新鲜。似乎通过设置一定数量的前向缓冲区,它会停止继续刷新并填充缓冲区。
    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 2018-11-06
    • 2012-06-10
    • 2016-06-03
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多