gpt4 book ai didi

ios - 如何在 AVSpeechUtterance 之后始终停止 AVAudioSession

转载 作者:可可西里 更新时间:2023-11-01 03:59:17 28 4
gpt4 key购买 nike

我想要做的是让我的应用程序在后台音频应用程序播放音频时使用 AVSpeechSynthesizer 说出一段话。当我的应用程序说话时,我希望后台应用程序的音频“变暗”,然后在我的应用程序完成说话后恢复到原来的音量。

在我的 AudioFeedback 类中,我初始化并设置了 AVAudioSessions,如下所示:

    self.session = [AVAudioSession sharedInstance];
NSError *error;
[self.session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];

每当我想说新的话语时,我都会执行以下操作。我遵循了 An issue with AVSpeechSynthesizer, Any workarounds? 的建议每次都创建一个新的 AVSpeechSynthesizer 以“确保”总是收到取消(它似乎有效,我不确定为什么)。

- (AVSpeechUtterance *) utteranceWithString: (NSString *) string
{
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-ES"];
[utterance setRate:(AVSpeechUtteranceDefaultSpeechRate+AVSpeechUtteranceMinimumSpeechRate)/2.0];
return utterance;
}

- (void) sayString: (NSString *) string cancelPrevious: (BOOL) cancelPrevious
{
[self.session setActive:enabled error:nil];
if (cancelPrevious) {
AVSpeechSynthesizer *oldSynthesizer = self.voice;
self.voice = nil;
[oldSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
self.voice = [[AVSpeechSynthesizer alloc] init];
self.voice.delegate = self;
}

// Keep track of the final utterance, we'll use this to determine whether or not we should stop the audio session
self.finalUtterance = [self utteranceWithString:string];
[self.voice speakUtterance:self.finalUtterance];
}

在我的 AVSpeechSynthesizer 委托(delegate)方法中,如果当前的 AVSpeechSynthesizer 和当前的 AVSpeechUtterance 与最后一个已知的合成器和话语相匹配,我会检查是否应该停止 Audio Session 以使背景音频恢复正常音量。

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSError *error;
// Only stop the audio session if this is the last created synthesizer
if (synthesizer == self.voice && self.finalUtterance == utterance) {
if ([self.session setActive:enabled error:&error]]) {
NSLog(@"Stopped the audio session: Speech synthesizer still speaking %d", synthesizer.speaking);
} else {
NSLog(@"ERROR failed to stop the audio session: %@. Speech synthesizer still speaking %d", error, synthesizer.speaking);
}
}
}

我遇到的问题是,有时 Audio Session 会毫无问题地停止,而其他时候, Audio Session 将无法停止并出现以下错误:

Error Domain=NSOSStatusErrorDomain Code=2003329396 "The operation couldn’t be completed. (OSStatus error 2003329396.)"

我不确定如何保证我可以停止 AVAudioSession。只要我无法停止 Audio Session ,我就会尝试继续调用 [[AVAudioSession sharedInstance] setActive:NO er​​ror:&error],但这似乎不起作用。任何帮助将不胜感激。谢谢!

最佳答案

我发现如果我在几秒钟后重试它就会成功。我正在使用此代码。

-(void)configureAVAudioSession:(bool)active {

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

if (deactivationAttempt < 3) { // This doesn't always work first time, but don't want to keep trying forever if it decides not to work ever.

NSError *activationError = nil;
success = [audioSession setActive:active error:&activationError];

if (!success) {
NSLog(@"(de)activation ERROR");
++deactivationAttempt;
[self performSelector:@selector(configureAVAudioSession:) withObject:false afterDelay:2.0];
}
else { // Success!
deactivationAttempt = 0;
}
}
else { // Failed, but reset the counter in case we have more luck next time
deactivationAttempt = 0;
}
}

来自文档:

Deactivating your session will fail if any associated audio objects (such as queues, converters, players or recorders) are currently running.

我想从队列中清除话语需要一段时间。

关于ios - 如何在 AVSpeechUtterance 之后始终停止 AVAudioSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21893007/

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