【问题标题】:AVPlayer doesn't resume playback on endinterruption on iPhone but does so on iPadAVPlayer 不会在 iPhone 上的 endinterruption 时恢复播放,但在 iPad 上会这样做
【发布时间】:2012-09-17 14:42:39
【问题描述】:

我正在为 iPhone 和 iPad 编写一个广播应用程序,在处理暂停和播放中断的音频时遇到了一些奇怪的行为。我正在使用 AVAudioSessionDelegate 方法 beginInterruptionendInterruption 分别到 pauseplay AVPlayer。以下是相关的play 代码。

现在,以下情况似乎一直在发生:

  1. 在 iPad 上:如果我强制中断(Facetime 通话),beginInterruption 会按预期调用并停止播放。如果中断停止,则按预期调用endInterruption,并按预期恢复播放。
  2. 在 iPhone 上:按下播放和暂停按钮,触发 pauseplaybeginInterruptionendInterruption 完全相同。播放按预期进行。
  3. 在 iPhone 上:强制中断(通过拨打电话),按预期拨打endInterruption 并按预期暂停播放。然而,当中断结束时,beginInterruption 按预期调用,play 按预期调用,它实际上到达并执行了[self.player play],但播放没有恢复!我什么也没听到。

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

Play代码

- (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];
    }
}}

【问题讨论】:

    标签: iphone ios avplayer avaudiosession


    【解决方案1】:

    我有同样的问题。在我实施远程控制事件处理后问题消失了。我在开始播放时拨打了[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    【讨论】:

      【解决方案2】:

      事实证明,这是 iOS 中的一个已知错误,它需要在 applicationDidBecomeActive 中进行一些仔细的工作来处理 beginInterruption。很遗憾,我想不出另一种解决方案。

      【讨论】:

      • 嘿,您能否就您提到的解决方法给出一些指导?遇到类似问题,找不到解决办法
      • @gavrix 基本上使用了很多状态管理来跟踪诸如“正在播放”、“暂停”、“中断呼叫”等使用布尔值。当应用程序重新回到前台时,代码会检查所有状态以检查它应该做什么。基本上是非常容易出错的行为,并且非常依赖于你所做的事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多