Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play AVSpeechSynthesizer through call receiver speaker?

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 ?

like image 699
Kishore Kumar Avatar asked Jul 27 '16 07:07

Kishore Kumar


1 Answers

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];
}
like image 176
foundry Avatar answered Sep 28 '22 20:09

foundry