我想在调用iOS睡眠定时器时停止我的音频应用。
就像潘多拉的应用程序一样 http://help.pandora.com/customer/portal/articles/24324-ios-sleep-timer-with-pandora
点击时钟应用,点击计时器,选择时间,点击计时器结束时,点击停止 打
如果Pandora应用程序正在运行,它将会睡眠。
我可以看到inInterruptionState == kAudioSessionBeginInterruption在iOS睡眠定时器结束时被调用,但是如何检测它是睡眠定时器还是只是打电话等中断?
这是我的代码。 目前,我的应用程序在iOS睡眠计时器结束后再次开始播放。
// Audio Interruption Listener
void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {
if (inInterruptionState == kAudioSessionBeginInterruption) {
[[DOSpeechManager sharedInstance] audioSessionBeginInterruption];
}
if (inInterruptionState == kAudioSessionEndInterruption) {
[[DOSpeechManager sharedInstance] audioSessionEndInterruption];
}
}
- (void)audioSessionBeginInterruption {
if ([_MyAcaTTS isSpeaking] && [_MyAcaTTS isPaused] == NO) {
[_MyAcaTTS pauseSpeakingAtBoundary:AcapelaSpeechImmediateBoundary];
[self setAudioSettionStatus:NO];
_audioInterruptedWhileSpeaking = YES;
}
}
- (void)audioSessionEndInterruption {
if (_audioInterruptedWhileSpeaking) {
[self setAudioSettionStatus:YES];
[_MyAcaTTS continueSpeaking];
}
}
- (void)setAudioSettionStatus:(BOOL)status {
AudioSessionSetActive(status);
[_MyAcaTTS setActive:status];
//cancel audio interrupted flag
if (status) {
_audioInterruptedWhileSpeaking = NO;
}
}
答案 0 :(得分:1)
诀窍不是要检测中断的来源,而是要知道你的应用程序是否应该在中断后恢复。
AVAudioSession API将在音频会话中断时发送通知。在此通知中,操作系统会“提示”应用程序是否应该恢复播放。
见下文:
//Add notification observer
__weak typeof(self) weakSelf = self;
self.audioSessionInterruptionNotification =
[[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSNumber* interruptionType = note.userInfo[AVAudioSessionInterruptionTypeKey];
NSNumber* interruptionOption = note.userInfo[AVAudioSessionInterruptionOptionKey];
BOOL shouldResume = interruptionOption.integerValue == AVAudioSessionInterruptionOptionShouldResume;
switch (interruptionType.integerValue) {
case AVAudioSessionInterruptionTypeBegan:
[weakSelf beginInterruption];
break;
case AVAudioSessionInterruptionTypeEnded:
[weakSelf endInterruption:shouldResume];
break;
default:
break;
}
}];
}