Search code examples
objective-cios7text-to-speech

TTS and Music simultaneously on iOS7?


I'm wondering if its possible to have music playback and some kind of text-to-speech engine to work simultaneously on iOS7. I'm using the new built in API from Apple and either one is working, not both. Any idea someone? My TTS playback code, also working in background

 -(void)speak:(NSString*)string
{
   AVAudioSession *audioSession = [AVAudioSession sharedInstance];

   NSError *setCategoryError = nil;
   [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

   NSError *activationError = nil;
   [audioSession setActive:YES error:&activationError];

   AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
   utterance.rate = 0.3f;
   utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:[AVSpeechSynthesisVoice currentLanguageCode]];

   AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
   [synth speakUtterance:utterance];

}

Solution

  • Ok, found it myself reading the docs.

    Using

    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback
      withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];
    

    does mix audio playback with the tts engine.