AVPlayer在iPhone上的中断时不会恢复播放,但会在iPad上播放 [英] AVPlayer doesn't resume playback on endinterruption on iPhone but does so on iPad

查看:85
本文介绍了AVPlayer在iPhone上的中断时不会恢复播放,但会在iPad上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone和iPad编写一个收音机应用程序,并且在处理暂停和播放音频时会遇到一些奇怪的行为。我正在使用AVAudioSessionDelegate方法 beginInterruption endInterruption 分别暂停播放 AVPlayer。以下是相关的播放代码。

I'm writing a radio app for both iPhone and iPad and am running into some weird behaviour when handling pausing and playing the audio with interruptions. I'm using the AVAudioSessionDelegate methods beginInterruption and endInterruption to respectively pause and play the AVPlayer. Below is the relevant play code.

现在,以下情况似乎始终如一:

Now, the following situations seem to happen consistently:


  1. 在iPad上:如果我强行中断(Facetime通话), beginInterruption 按预期调用,播放停止。如果中断停止,将按预期调用 endInterruption ,播放将按预期恢复。

  2. 在iPhone上:按播放和暂停按钮,触发暂停播放完全相同> beginInterruption endInterruption 会。播放按预期运行。

  3. 在iPhone上:强制中断(通过拨打电话),拨打 endInterruption 正如预期的那样,播放暂停按预期进行。但是,当中断完成时,按预期调用 beginInterruption ,按预期调用 play it实际上到达并执行 [self.player play] ,但播放不会恢复!我什么也听不见。

  1. On iPad: If I force an interruption (Facetime call), beginInterruption is called as expected and playback stops. If the interruption stops, endInterruption is called as expected, and the playback resumes as expected.
  2. On iPhone: Pressing the play and pause button, triggers pause and play exactly the same as beginInterruption and endInterruption would. Playback behaves as expected.
  3. On iPhone: Forcing an interruption (by calling the phone), calls endInterruption as expected and playback pauses as expected. However, when the interruption is finished, beginInterruption is called as expected, play is called as expected, it actually reaches and executes the [self.player play] line, but playback does not resume! I hear nothing.

上面的情况3非常奇怪,所以我想知道我是否可能忽略了一些东西。有什么想法吗?

Situation 3 above is extremely weird, so I'm wondering if I may have overlooked something. Any ideas?

播放代码

Play code

- (void)play { 
NSError* error = nil;
AVKeyValueStatus keyStatus = [currentAsset statusOfValueForKey:@"tracks" error:&error];
if(error){
    DLog(@"Error %@", [error localizedDescription]);
}
else {
    DLog(@"Current Key Status: %i", keyStatus);
    if(keyStatus == AVKeyValueStatusLoaded){
        DLog(@"Continue playing source: %@", self.source);
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
        if (error) {
            DLog(@"Error during play while setting AVAudioSessionCategory: %@", [error localizedDescription]);
        }
        [[AVAudioSession sharedInstance] setActive:YES error:&error];
        if (error) {
            DLog(@"Error during play while setting AVAudioSessionCategory: %@", [error localizedDescription]);
        }
        [[AVAudioSession sharedInstance] setDelegate:self];
        if(backgroundTaskID != UIBackgroundTaskInvalid){
            [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
        }
        backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

        [self.player play];
        [self setStatus:kNPOMediaPlayerStatusPlaying];
    }
    else {
        DLog(@"PlaySource: %@", self.source);
        [self playSource:self.source];
    }
}}


推荐答案

事实证明,这是iOS中的已知错误,需要在applicationDidBecomeActive中进行一些仔细的解决方法来处理beginInterruption。可悲的是,我无法找到另一种解决方案。

It turns out that this is a known bug in iOS, which requires some careful work-arounds in the applicationDidBecomeActive to handle beginInterruption. Sadly, I couldn't figure out another solution.

这篇关于AVPlayer在iPhone上的中断时不会恢复播放,但会在iPad上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆