【问题标题】:iOS enable to play audio after return from other music appiOS 允许从其他音乐应用返回后播放音频
【发布时间】:2017-08-04 12:27:58
【问题描述】:

我正在开发音乐应用程序。

这是我要解决的情况: 我打开 AppleMusic 应用程序 > 播放一些东西 > 打开我的应用程序 > 单击播放 > 音乐不播放。为什么以及如何解决?

我用过但还是不行

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    UIApplication.shared.beginReceivingRemoteControlEvents()
}

【问题讨论】:

    标签: ios swift uiviewcontroller avaudioplayer audio-player


    【解决方案1】:

    此代码显示您是否有其他应用正在后台播放声音

    var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying
    

    如果是这样,试试这个:

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2015-05-01
      • 1970-01-01
      • 2011-10-06
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多