【发布时间】:2012-09-17 14:42:39
【问题描述】:
我正在为 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];
}
}}
【问题讨论】:
标签: iphone ios avplayer avaudiosession