Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 更改音频会话类别后,MPRemoteCommandCenter不可用_Swift_Xcode_Avaudiosession_Mpremotecommandcenter_Avaudiosessioncategory - Fatal编程技术网

Swift 更改音频会话类别后,MPRemoteCommandCenter不可用

Swift 更改音频会话类别后,MPRemoteCommandCenter不可用,swift,xcode,avaudiosession,mpremotecommandcenter,avaudiosessioncategory,Swift,Xcode,Avaudiosession,Mpremotecommandcenter,Avaudiosessioncategory,我的应用程序可以选择将声音与其他应用程序混合。 据苹果称,MPRemoteCommandCenter仅在应用程序不允许混合时可用 在我的应用程序中,当用户点击按钮更改mixWithOthers设置时,音频会话将相应地设置 但是,即使用户切换回“不允许混合”,RemoteCommandCenter也不会显示在锁定屏幕中,直到应用程序从缓存中删除(刷起)并重新启动 有没有一种方法可以在不重新启动应用程序的情况下实现所需的行为 func application(_ application: UIApp

我的应用程序可以选择将声音与其他应用程序混合。 据苹果称,MPRemoteCommandCenter仅在应用程序不允许混合时可用

在我的应用程序中,当用户点击按钮更改mixWithOthers设置时,音频会话将相应地设置

但是,即使用户切换回“不允许混合”,RemoteCommandCenter也不会显示在锁定屏幕中,直到应用程序从缓存中删除(刷起)并重新启动

有没有一种方法可以在不重新启动应用程序的情况下实现所需的行为

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
}

如果无法解决此问题,是否有人可以确认上述行为?在开始新类别或开始播放
try audioSession.setActive(false)之前,是否尝试将会话设置为非活动状态
顺便说一下,混合时,
MPRemoteCommandCenter
不可用,因此切换时需要再次调用
beginReceivingRemoteControlEvents
(我猜您已经知道了)感谢您的建议,是的,再次更改会话类别后,我会调用beginReceivingRemoteControlEvents,虽然我认为可能没有必要。关于在更改之前将音频会话设置为非活动状态,这并不能解决问题。