5

My app has got the option to allow its sound to be mixed with other apps. According to Apple, MPRemoteCommandCenter is only available when apps do not allow for mixing.

Inside my app, when the user taps the button to change the mixWithOthers setting, the audio session is set accordingly.

However, even when the user switches back to not allow mixing anymore MPRemoteCommandCenter will not show up in lock screen until the app has been removed from cache (swiped up) and started again.

Is there a way to achieve the desired behaviour without having to re-start the app?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
  UIApplication.shared.beginReceivingRemoteControlEvents()
}

var isMixWithOthersAllowed: Bool

func startProcess() {
  setAudioSession {
   setupMediaControls()
  }
}

func setAudioSession(completion: @escaping () -> Void) {
  let audioSession = AVAudioSession.sharedInstance()

  do {

    if isMixWithOthersAllowed {
      try audioSession.setCategory(.playback, options: [.mixWithOthers])
      /* The remote command center will not be available when mixing with others, 
        as stated by Apple in the docs. */
    } else {
      try audioSession.setCategory(.playback) 
      /* The remote command center should be available when switching back to 
         this category, but it will only show up after the app has been killed 
         and started fresh again. I'd like it to be available without restarting 
         the application. */
    }

    try audioSession.setActive(true)

  } catch let error as NSError {
    configureAudioSessionError = error
  }
  assert(configureAudioSessionError == nil, "Create audio session failed")

  completion()
}


func setupMediaControls() {
  let commandCenter = MPRemoteCommandCenter.shared()

  commandCenter.playCommand.isEnabled = true
  commandCenter.pauseCommand.isEnabled = true
  commandCenter.nextTrackCommand.isEnabled = true
  commandCenter.previousTrackCommand.isEnabled = true

  // Setup handlers for commands
  // ...

  setupNowPlaying()
}

func setupNowPlaying() {
  // Configure audio track metadata and update UI elements
}
nontomatic
  • 2,003
  • 2
  • 24
  • 38
  • If there isn't a way around this, can someone confirm the behaviour described above? – nontomatic May 22 '20 at 16:42
  • 1
    did you try setting the session to inactive before starting a new category or starting to play `try audioSession.setActive(false)` – zombie May 25 '20 at 14:32
  • by the way from what you're saying `MPRemoteCommandCenter` is not available when mixing so when switching you need call `beginReceivingRemoteControlEvents` again (I'm guessing you know that already) – zombie May 25 '20 at 14:45
  • Thank you for the suggestions, yes, I do call beginReceivingRemoteControlEvents after changing the session category again, although I think it may not be necessary. Regarding setting the audio session inactive before changing, it does not solve the problem. – nontomatic May 25 '20 at 16:22

0 Answers0