title: AVPlayer & MPMoviePlayerController Can't Be Muted --- Forcing Background Audio Dim
body: <p>My app plays a video and occasionally has verbal queues. When there is nothing being said by the app, I want the audio (in the background say from Spotify or iTunes) to come back to full volume. When there is audio I want the audio to dim.</p>
  
  <p>I have set the audio session to <code>DuckOthers</code>.</p>
  
  <p>The problem is, whenever I restore the background audio:</p>
  
  <pre><code>class func restoreBackgroundAudio()
  {
      do {
          try AVAudioSession.sharedInstance().setActive(false)
      } catch _ {
          print("ERROR restoring background audio")
      }
  }
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>[0x1a03a9000] AVAudioSession.mm:697: -[AVAudioSession
    setActive:withOptions:error:]: Deactivating an audio session that has
    running I/O. All I/O should be stopped or paused prior to deactivating
    the audio session.</p>
  </blockquote>
  
  <p>The problem is that the video file that is playing seems to have an audio that I cannot mute.</p>
  
  <p>I have tried the following:</p>
  
  <ol>
  <li><p>I removed the audio track from the video and tried playing it in an MPMoviePlayerController</p>
  
  <pre><code>_movie                                = MPMoviePlayerController()
  _movie.controlStyle                   = MPMovieControlStyle.None
  _movie.view.frame                     = _movieView.frame
  _movie.repeatMode                     = MPMovieRepeatMode.One
  _movie.backgroundView.backgroundColor = UIColor.whiteColor()
  _movie.contentURL = NSURL(fileURLWithPath: documentsDirectory.stringByAppendingString("/\(videoName)"))
   _movie.play()
  </code></pre></li>
  </ol>
  
  <p>Every time the movie plays its loop it forces the background audio to dim even though I am  not setting <code>AVAudioSession.sharedInstance().setActive(true)</code></p>
  
  <ol start="2">
  <li><p>Making my own AVPlayerItem and AVPlayer, and manually muting the audio tracks inside the AVPlayerItem as well as setting the AVPlayer to muted:</p>
  
  <pre><code>    var asset = AVURLAsset(URL: _movieURL)
      var audioTracks = asset.tracksWithMediaType(AVMediaTypeAudio)
      // Set the volume of ANY potential audio track to 0
      var allAudioParams:[AVMutableAudioMixInputParameters] = []
      for track:AVAssetTrack in audioTracks
      {
          var audioInputParams = AVMutableAudioMixInputParameters()
          audioInputParams.setVolume(0.0, atTime: kCMTimeZero)
          audioInputParams.trackID = track.trackID
          allAudioParams.append(audioInputParams)
      }
  
      var muteTrack = AVMutableAudioMix()
      muteTrack.inputParameters = allAudioParams
  
      var item:AVPlayerItem = AVPlayerItem(URL: _movieURL)
      // Assign the muted audio track
      item.audioMix = muteTrack
  
      _avPlayer = AVPlayer(playerItem: item)
      // Also set the AVPlayer to be muted
      _avPlayer.muted = true
      let playerLayer = AVPlayerLayer(player: _avPlayer)
      playerLayer.frame = _movie.view.frame
      layer.addSublayer(playerLayer)
  
      _avPlayer.play() // ALWAYS DIMS BACKGROUND AUDIO!
  </code></pre></li>
  </ol>
  
  <p>Again, once the video loops it always dims the background audio. I tried hacking it and doing a <code>restoreBackgroundAudio()</code> call right after I hit the play button, but I get a bad fade in and out and in and out effect like its trying to restore the background audio but the active video player is forcing the dim as an override.</p>
  
  <p>If you have any suggestions on what else I can do to either actually mute a video thats playing or play a video that doesnt dim the background I would appreciate it.</p>