我想要做的是让我的应用在背景音频应用播放音频时使用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委托方法中,如果当前的AVSpeechSynthesizer和当前的AVSpeechUtterance与最后已知的合成器和话语匹配,我会检查是否应该停止音频会话以将背景音频恢复为正常音量。
-(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);
}
}
}
我遇到的问题是,有时,音频会话会在没有问题的情况下停止,有时,音频会话将无法停止并出现以下错误:
错误域= NSOSStatusErrorDomain代码= 2003329396“操作无法完成。(OSStatus错误2003329396。)”
我不确定如何保证我可以停止AVAudioSession。只要我无法停止音频会话,我就试着继续呼叫[[AVAudioSession sharedInstance] setActive:NO error:&error]
,但这似乎不起作用。任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
我发现如果我在几秒钟内再试一次它会起作用。我正在使用此代码。
-(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;
}
}
来自文档:
如果当前正在运行任何关联的音频对象(例如队列,转换器,播放器或录像机),则取消激活会话将失败。
我想从队列中清除话语需要一段时间。
答案 1 :(得分:0)
我遇到了同样的问题,我相信我找到了原因。对我而言,如果我所说的短语太短,则会给出错误。如果它“足够长”,则没有错误。
我测试了
NSString *phraseToSay = [NSString stringWithFormat:@"One Two Three Four Five"]; // Gives the error
NSString *phraseToSay = [NSString stringWithFormat:@"One Two Three Four Five Six"]; // No error
我找到的唯一解决方案是使短语更长。