title: AVAudioSession mixing with AVSpeechUtterance
body: <p>I have an issue where I would like to have the iPod playing music in the background and in my application I would like to have <code>AVSpeechUtterance</code> lower the volume of the iPod, play a string of text and then I would like to have the iPod resume playing.  I have tried several things but never had AVAudioSession and <code>AVSpeechUtterance</code> work well together.  I can get the iPod to stop, pause for a bit and then start playing again.  I can see my string being passed to <code>AVSpeechUtterance</code>, but it does not take precedence to be played over the speaker.  Here is my code:</p>
  
  <p>View did load:</p>
  
  <pre><code>   - (void)viewDidLoad
     {
              //set the audio session for my app to play from the TTS library
              NSError *error;
              audioSession = [AVAudioSession sharedInstance];
              [audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:1  error:&amp;error];
              [audioSession setActive:YES error:&amp;error];
              if(error){
                  NSLog(@"Audio Error %@", error);
              }
  
              syn = [[AVSpeechSynthesizer alloc] init];
              syn.delegate = self;
  
              iPodMusicPlayer = [[MPMusicPlayerController alloc] init];
  
     }
  </code></pre>
  
  <p>Here is my speech function:</p>
  
  <pre><code>   -(void)speech:(NSString *)string
     {
               if(audioSession.isOtherAudioPlaying){
                   wasPlaying = YES;
                   NSLog(@"Other audio is playing");
                   [iPodMusicPlayer pause];
               }
  
               AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
               utterance.rate = 0.3f;
               utterance.volume = 1.0;
               [syn speakUtterance:utterance];
  
     }
  </code></pre>
  
  <p><code>AVSpeechSynthesizer</code> Delegate method that gets called after the speech is finished</p>
  
  <pre><code>   - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
     {
           if(wasPlaying){
              NSLog(@"Start Playing");
              [iPodMusicPlayer play];
              wasPlaying = NO;
           }
     }
  </code></pre>
  
  <p>I reviewed the Apple docs on <code>AVAudioSession</code> <a href="https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html#//apple_ref/doc/constant_group/Audio_Session_Categories" rel="nofollow">here</a> and it looks like I have everything implemented correctly.  Does anyone have any idea where I went wrong?</p>