В зависимости от того, как ваш звук был остановлен (вы звонили [self.audioPlayer stop]
?), Вам, возможно, придется позвонить [self.audioPlayer prepareToPlay]
, прежде чем звонить play
.
Я считаю, что вы должны сделать, это следующее:
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
{
[self.audioPlayer pause];
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
{
[self.audioPlayer play];
}
По моему опыту, если вы звоните stop
вы тогда должны вызвать prepareToPlay
перед вызовом play
снова.
EDIT:
С другой стороны, вам может понадобиться для обработки прерываний через 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
Правильно. Поэтому я думаю, что вам нужно приостановить звук в 'audioPlayerBeginInterruption'. Телефонный вызов вызывает ваш звук _stop_. Вы хотите, чтобы он сам _pause_, поэтому вы можете перезапустить его, когда прерывание завершено. –
Я тоже пробовал это, но все равно нет звука – user1120133