gpt4 book ai didi

iphone - 如何在调用 iOS sleep 定时器时停止音频

转载 作者:行者123 更新时间:2023-11-29 13:11:44 24 4
gpt4 key购买 nike

我想在调用 iOS sleep 定时器时停止我的音频应用。

就像潘多拉应用程序一样。
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.

我可以看到 inInterruptionState == kAudioSessionBeginInterruption 在 iOS sleep 定时器结束时被调用,但我如何检测它是 sleep 定时器还是只是电话调用之类的中断?

这是我的代码。目前,我的应用程序只是在 iOS sleep 定时器结束后再次开始播放。

// 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;
}
}

最佳答案

诀窍不是检测中断源,而是了解您的应用程序是否应在中断后恢复。

AVAudioSession API 会在 Audio Session 中断时发送通知。在此通知中,操作系统会就应用是否应恢复播放给出“提示”。

见下文:

    //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;
}
}];
}

关于iphone - 如何在调用 iOS sleep 定时器时停止音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com