My application wants to play some text when it is available, and if there is some music playing in the background I want to lower the voice for the music while my app is playing its text, what I did is:
[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
The option AVAudioSessionCategoryOptionDuckOthers
will lower the music volume while my app is playing its text.
Then play the text with the speechSynthesizer
, after that in:
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)
utterance I do:
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
so the music will be back to it original volume.
The problem by setting the session active to NO, I am loosing the volume control (the iPhone hardware volume control the one in the left side of the phone). i.e I can not upper or lower the volume for my app unless there is a text actively playing in my app at the moment I am changing the volume.