Reputation: 434
I am calling iOS text to speech in a pretty standard way:
static AVSpeechSynthesizer* synthesizer = NULL;
//...
+(void)readText:(NSString*)text
{
if(synthesizer == NULL)
synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-FR"];
[synthesizer speakUtterance:utterance];
}
It does the job, except for one issue: when the ringer is muted on device, text to speech is muted too. How do I make it work even when the ringer is muted?
Upvotes: 1
Views: 792
Reputation:
Thanks to Leo Cavalcante. After setting AVAudioSession setCategory and others, it solves my problem.
I have added swift code below.
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
// try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
Upvotes: 1
Reputation: 434
iOS audio session categories are the correct answer for this: https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html.
The default value is AVAudioSessionCategorySoloAmbient
, but if the audio functionality is central for the app (our case), it should be set to AVAudioSessionCategoryPlayback
:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Upvotes: 3
Reputation: 1303
Just unmute the device
static AVSpeechSynthesizer* synthesizer = NULL;
//...
+(void)readText:(NSString*)text
{
if(synthesizer == NULL)
synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-FR"];
//Add the following code to active audio
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
//magic is here
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
//speech your text
[synthesizer speakUtterance:utterance];
}
Upvotes: 1