【问题标题】:background device music gets stopped as app starts ios当应用程序启动 ios 时,后台设备音乐停止
【发布时间】:2015-01-28 14:18:40
【问题描述】:

我在 iphone 中播放音乐,当我运行我的应用程序时,它不应该停止背景设备音乐,应该停止它自己的音乐,而不是背景音乐停止。我想做和糖果迷应用程序一样的事情。我应该怎么做才能解决这个问题?请帮帮我

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

【问题讨论】:

  • 您需要适当地设置应用的音频会话类别和选项。 developer.apple.com/library/prerelease/ios/documentation/…
  • 它已通过以下代码解决:- AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil) == true 我将我的电影播放器​​控制器放置在哪里播放

标签: ios swift avaudioplayer background-music


【解决方案1】:

我建议查看Sameer's answer to this for Swift 2.0, just a few months back. 与 Swift 2.0 完美配合。

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

【讨论】:

    【解决方案2】:

    由于默认为 AVAudioSessionCategorySoloAmbient,您需要为您的应用设置正确的类别并激活它。您可以使用 AVAudioSessionCategoryAmbient 来混合音频。

    在你的 AppDelegate.swift 中添加这个:

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

    享受吧!

    【讨论】:

      【解决方案3】:

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

      更多详情: https://developer.apple.com/documentation/avfoundation/avaudiosession

      【讨论】:

        【解决方案4】:

        Xcode 10.3
        斯威夫特 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....中设置这个功能。

        【讨论】:

        • 你为什么使用 mixWithOthers?而不是环境?
        • @Emil AVAudioSessionCategoryOptionMixWithOthers控制当您的应用的音频会话处于活动状态时是否会中断或混合其他活动的音频应用。 MixWithOthers 仅对 AVAudioSessionCategoryPlayAndRecordAVAudioSessionCategoryPlaybackAVAudioSessionCategoryMultiRoute 有效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多