我正在为iPhone和iPad编写一个广播应用程序,在处理暂停和播放中断音频时遇到了一些奇怪的行为。我正在使用AVAudioSessionDelegate方法beginInterruption和endInterruption分别用于pause和play AVPlayer。下面是相关的play代码。
现在,以下情况似乎一直在发生:
beginInterruption并停止播放。如果中断停止,则按预期调用endInterruption,并按预期恢复播放。pause和play与beginInterruption和endInterruption完全相同。播放的行为与预期的一样。endInterruption,并按预期暂停播放。但是,当中断结束时,按预期调用beginInterruption,按预期调用play,它实际上到达并执行[self.player play]行,但回放没有恢复!--我什么也没听到。上面的情况非常奇怪,所以我想知道我是否忽略了什么。有什么想法吗?
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];
}
}}发布于 2012-12-12 13:03:05
事实证明,这是iOS中已知的一个bug,需要在applicationDidBecomeActive中进行一些仔细的工作来处理beginInterruption。遗憾的是,我想不出另一个解决办法。
发布于 2013-04-21 20:36:33
我也有过同样的问题。实现远程控制事件处理后,问题消失。在开始播放时,我调用了[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];。
https://stackoverflow.com/questions/12461691
复制相似问题