问题描述
我正在为iPhone和iPad编写一个无线电应用,并在处理暂停时播放并播放中断音频时,我正在运行一些奇怪的行为.我正在使用avAudioSessionDelegate方法beginInterruption和endInterruption分别pause和play avplayer.以下是相关的play代码.
现在,以下情况似乎一致:
-
iPad上的
- :如果我强制中断(FaceTime呼叫),beginInterruption称为预期和播放停止.如果中断停止,endInterruption按预期调用,并且播放按预期恢复. iPhone上
- :按播放和暂停按钮,触发pause和play与beginInterruption和endInterruption完全相同.播放行为正常. 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]; } }}
推荐答案
使这是iOS中的已知错误,这需要在ApplicationDidBecomecctive中仔细研究围绕ApplicationDidBecomeCtive来处理BeartInteruption.可悲的是,我无法弄清楚另一种解决方案.
其他推荐答案
我有同样的问题.在我实现遥控事件处理后,问题消失了.启动播放时,我叫[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];.