根据您的音频停止方式(您是否拨打[self.audioPlayer stop]
?),您可能需要在致电play
之前致电[self.audioPlayer prepareToPlay]
。
我相信你应该做的是以下几点:
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
{
[self.audioPlayer pause];
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
{
[self.audioPlayer play];
}
以我的经验,如果你打电话stop
,那么你必须调用prepareToPlay
可以再次调用play
之前。
编辑:
或者,你可能需要通过AudioSession直接处理中断。
您的应用程序应该初始化AudioSession,像这样:
AudioSessionInitialize(NULL, NULL, AudioInterruptionListener, NULL);
然后,实施AudioInterruptionListener
您@implementation
/@end
块之外,这样的事情:
#define kAudioEndInterruption @"AudioEndInterruptionNotification"
#define kAudioBeginInterruption @"AudioBeginInterruptionNotification"
void AudioInterruptionListener (
void *inClientData,
UInt32 inInterruptionState
)
{
NSString *notificationName = nil;
switch (inInterruptionState) {
case kAudioSessionEndInterruption:
notificationName = kAudioEndInterruption;
break;
case kAudioSessionBeginInterruption:
notificationName = kAudioBeginInterruption;
break;
default:
break;
}
if (notificationName) {
NSNotification *notice = [NSNotification notificationWithName:notificationName object:nil];
[[NSNotificationCenter defaultCenter] postNotification:notice];
}
}
回到你的Objective-C ,您需要收听此代码可能发布的通知,例如:
// Listen for audio interruption begin/end
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(beginAudioInterruption:)
name:kAudioBeginInterruption object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(endAudioInterruption:)
name:kAudioEndInterruption object:nil];
和:
-(void)beginAudioInterruption:(id)context
{
[self.audioPlayer pause];
}
-(void)endAudioInterruption:(id)context
{
[self.audioPlayer play];
}
给一个漩涡。 :-)
由于电话音频自己暂停并且电话通话结束后没有音频。 – user1120133
对。所以我认为_you_需要暂停'audioPlayerBeginInterruption'中的音频。电话正在导致您的音频停止_stop_。您希望自己暂停使用,以便在中断结束后重新启动。 –
我也尝试过,但仍然没有音频 – user1120133