如何通过呼叫接收器扬声器播放AVSpeechSynthesizer?
问题描述:
我喜欢通过呼叫接收器扬声器播放音频,目前我正在使用它来播放某些文本作为音频。如何通过呼叫接收器扬声器播放AVSpeechSynthesizer?
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];
}
答
这为我工作:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker])
但仍然通过其受话器扬声器音箱上运行的兄弟http://paste.ofcode.org/uEi8M5FjcGhB97NurcEEsM –
其没有运行 –
@KishoreKumar - 看到我的更新 - 你需要检查你的audioSession类别是否正确 – foundry