应用程序启动ios时后台设备音乐停止

应用程序启动ios时后台设备音乐停止,ios,swift,avaudioplayer,background-music,Ios,Swift,Avaudioplayer,Background Music,我在iphone中播放音乐,当我运行我的应用程序时,它不应该停止背景设备音乐,应该停止自己的音乐,而应该停止背景音乐。我想做同样的糖果粉碎应用程序。我应该如何解决这个问题?请帮帮我 var isOtherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying println("isOtherAudioPlaying>>> \(isOtherAudioPlaying)") i

我在iphone中播放音乐,当我运行我的应用程序时,它不应该停止背景设备音乐,应该停止自己的音乐,而应该停止背景音乐。我想做同样的糖果粉碎应用程序。我应该如何解决这个问题?请帮帮我

    var isOtherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying
      println("isOtherAudioPlaying>>> \(isOtherAudioPlaying)")
      if(isOtherAudioPlaying == false)
           {
            audioPlayer1!.play()
           }

由于默认设置为AVAudioSessionCategorySoloAmbient,因此您需要为应用程序设置适当的类别并激活它。您可以使用AvaudioSessionCategoryBient来混合音频

将此添加到AppDelegate.swift中:

func applicationDidBecomeActive(application: UIApplication) {
  AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
  AVAudioSession.sharedInstance().setActive(true, error: nil)
  // ...
}

享受吧

我建议您使用Swift 2.0完美地检查工作

let sess = AVAudioSession.sharedInstance()
if sess.otherAudioPlaying {
    _ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: []) //
    _ = try? sess.setActive(true, withOptions: [])
}

延迟
AVAudioSession.sharedInstance().setActive(true,错误:nil)
直到需要开始播放音频时。应用程序启动后立即启动会话会停止在其他应用程序上播放

更多详情:

Xcode 10.3
Swift 4.2
在iPhoneX(12.2)、iPhone5s(11.0.3)中测试

功能->在静音模式下播放音频+使用其他应用程序音频

这是密码

private func setupAudioSession() {
        //enable other applications music to play while quizView
        let options = AVAudioSession.CategoryOptions.mixWithOthers
        let mode = AVAudioSession.Mode.default
        let category = AVAudioSession.Category.playback
        try? AVAudioSession.sharedInstance().setCategory(category, mode: mode, options: options)
        //---------------------------------------------------------------------------------
        try? AVAudioSession.sharedInstance().setActive(true)
    }

我建议您在AppDelegate->didFinishLaunchingWithOptions中设置此功能。…

您需要适当设置应用程序的音频会话类别和选项。通过以下代码解决了此问题:-AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient,错误:nil)=在我放置movieplayercontroller播放的位置为true