I like to play audio through the call receiver speaker ,currently i am using this for play some text as a audio .
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:_strTextCheck];
AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
[syn speakUtterance:utterance];
I got but this is not for AVSpeechSynthesizer:
[AVAudioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
Correctly its working on a normal speaker but i want to make to play through call receiver speaker ,is it possible to do that ?
The default behaviour is to play through the call receiver. So if you unset the override - or don't set it in the first place - you should get the behaviour you are after:
[[AVAudioSession sharedInstance]
overrideOutputAudioPort:AVAudioSessionPortOverrideNone
error:nil];
Here is a full example. You need to also set the audioSession category.
- (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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With