title: AVAudioSession never stopped
body: <p>I'm trying to set my <code>AVAudioSession</code> to inactive to get back to normal state.</p>
  
  <p>My utterance function:</p>
  
  <pre><code>class SSpeech : NSObject, AVSpeechSynthesizerDelegate {
  
      var group = DispatchGroup();
      var queue = DispatchQueue(label: "co.xxxx.speech", attributes: [])
  
      class var sharedInstance: SSpeech {
          struct Static {
              static var instance: SSpeech?
          }
          if !(Static.instance != nil) {
              Static.instance = SSpeech()
          }
         return Static.instance!
      }
  
     required override init() {
         super.init();
         self.speechsynt.delegate = self;
     }
  
     deinit {
        print("deinit SSpeech")
     }
  
     let audioSession = AVAudioSession.sharedInstance();
     var speechsynt: AVSpeechSynthesizer = AVSpeechSynthesizer()
     var queueTalks = SQueue&lt;String&gt;();
  
     func pause() {
          speechsynt.pauseSpeaking(at: .word)
     }
  
     func talk(_ sentence: String, languageCode code:String = SUtils.selectedLanguage.code, withEndPausing: Bool = false) {
         if SUser.sharedInstance.currentUser.value!.speechOn != 1 {
             return
          }
  
          queue.async{            
              self.queueTalks.enQueue(sentence)
              do {
                  let category = AVAudioSessionCategoryPlayback;
                  var categoryOptions = AVAudioSessionCategoryOptions.duckOthers
                  if #available(iOS 9.0, *) {
                      categoryOptions.formUnion(AVAudioSessionCategoryOptions.interruptSpokenAudioAndMixWithOthers)
                   }
                  try self.audioSession.setCategory(category, with: categoryOptions)
                  try self.audioSession.setActive(true);
              } catch _ {
                  return;
              }
  
              self.utteranceTalk(sentence, initSentence: false, speechsynt: self.speechsynt, languageCode:code, withEndPausing: withEndPausing)
  
              do {
                  try self.audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
              } catch _ {
                  return;
              }
          }
      }
  
      func utteranceTalk(_ sentence: String, initSentence: Bool, speechsynt: AVSpeechSynthesizer, languageCode:String = "en-US", withEndPausing: Bool = false){
          if SUser.sharedInstance.currentUser.value!.speechOn != 1 {
              return
          }
  
          let nextSpeech:AVSpeechUtterance = AVSpeechUtterance(string: sentence)
          nextSpeech.voice = AVSpeechSynthesisVoice(language: languageCode)
          if !initSentence {
              nextSpeech.rate = 0.4;
          }
  
          if(withEndPausing){
              nextSpeech.postUtteranceDelay = 0.2;
          }
          speechsynt.speak(nextSpeech)
      }
  
  
      func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance:AVSpeechUtterance) {
          print("Speaker has finished to talk")
  
          queue.async {
                 do {
                     try self.audioSession.setActive(false, with: AVAudioSessionSetActiveOptions.notifyOthersOnDeactivation)
                 }
                 catch {}
             }
         }
      }
  }
  </code></pre>
  
  <p>My method is correctly called, but my <code>audioSession</code> still active when the <code>utterance</code> is finished. i've tried lot of thing but nothing work :(. </p>