[英]how to stop audio when iOS Sleep Timer gets called
I want to stop my audio app when iOS sleep timer gets called. 我想在iOS睡眠计时器被调用时停止我的音频应用程序。
Just like Pandora app. 就像Pandora应用一样。
http://help.pandora.com/customer/portal/articles/24324-ios-sleep-timer-with-pandora http://help.pandora.com/customer/portal/articles/24324-ios-sleep-timer-with-pandora
Tap the Clock app, Tap Timer, Select a time, Tap When Timer Ends, Tap Stop Playing
点按“时钟”应用程序,点按“计时器”,选择一个时间,点按“计时器结束时”,点按“停止播放”
This will sleep your Pandora app if it is running.
如果Pandora应用程序正在运行,它将使其休眠。
I can see inInterruptionState == kAudioSessionBeginInterruption gets called when iOS sleep timer ends, but how can I detect if it's sleep timer or just interruptions like phone call? 我可以看到inInterruptionState == kAudioSessionBeginInterruption在iOS睡眠计时器结束时被调用,但是如何检测它是睡眠计时器还是仅仅是像电话这样的中断?
Here is my codes. 这是我的代码。 Currently, my app just starts playing again after iOS sleep timer ends.
目前,我的应用只是在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;
}
}
The trick is not to detect the source of the interruption, but to know whether your app should resume after the interruption. 诀窍不是要检测中断源,而是要知道您的应用在中断后是否应继续运行。
The AVAudioSession API will send a notification when the audio session is interrupted. 音频会话中断时,AVAudioSession API将发送通知。 Within this notification, the OS gives a "hint" as to whether the app should resume playback or not.
在此通知中,操作系统会“提示”应用程序是否应继续播放。
See below: 见下文:
//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;
}
}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.