AVPlayer在iPhone上的中断时不会恢复播放,但会在iPad上播放 [英] AVPlayer doesn't resume playback on endinterruption on iPhone but does so on 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:
- 在iPad上:如果我强行中断(Facetime通话),
beginInterruption
按预期调用,播放停止。如果中断停止,将按预期调用endInterruption
,播放将按预期恢复。 - 在iPhone上:按播放和暂停按钮,触发
暂停
和播放
与完全相同> beginInterruption
和endInterruption
会。播放按预期运行。 - 在iPhone上:强制中断(通过拨打电话),拨打
endInterruption
正如预期的那样,播放暂停按预期进行。但是,当中断完成时,按预期调用beginInterruption
,按预期调用play
, it实际上到达并执行[self.player play]
行,但播放不会恢复!我什么也听不见。
- 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. - On iPhone: Pressing the play and pause button, triggers
pause
andplay
exactly the same asbeginInterruption
andendInterruption
would. Playback behaves as expected. - 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屋!