gpt4 book ai didi

ios - 如何通过调用接收器扬声器播放 AVSpeechSynthesizer?

转载 作者:可可西里 更新时间:2023-11-01 04:40:59 25 4
gpt4 key购买 nike

我喜欢通过调用接收器扬声器播放音频,目前我正在使用它来播放一些文本作为音频。

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:_strTextCheck];
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
[syn speakUtterance:utterance];

我明白了,但这不适用于 AVSpeechSynthesizer:

[AVAudioSession  overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

正确的是它在普通扬声器上工作,但我想通过调用接收器扬声器播放,是否可以这样做?

最佳答案

默认行为是通过调用接收器播放。因此,如果您取消设置覆盖 - 或者不首先设置它 - 您应该得到您想要的行为:

[[AVAudioSession sharedInstance] 
overrideOutputAudioPort:AVAudioSessionPortOverrideNone
error:nil];

这是一个完整的例子。您还需要设置 audioSession 类别。

- (void)playVoice {
[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];

//try one or the other but not both...

//[self playVoiceOnSpeaker:@"test test test"];

[self playVoiceOnReceiver:@"test test test"];

}

-(void)playVoiceOnSpeaker:(NSString*)str
{
[[AVAudioSession sharedInstance]
overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:nil];
[self playVoiceByComputer:str];
}

-(void)playVoiceOnReceiver:(NSString*)str
{
[[AVAudioSession sharedInstance]
overrideOutputAudioPort:AVAudioSessionPortOverrideNone
error:nil];
[self playVoiceByComputer:str];
}

-(void)playVoiceByComputer:(NSString*)str
{
AVSpeechUtterance *utterance =
[AVSpeechUtterance speechUtteranceWithString:str];
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
[syn speakUtterance:utterance];
}

关于ios - 如何通过调用接收器扬声器播放 AVSpeechSynthesizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38606831/

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