iOS 允许从其他音乐应用程序返回后播放音频

标签 ios swift uiviewcontroller avaudioplayer audio-player

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

这是我想要修复的情况: 我打开 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()
}

最佳答案

此代码会显示其他应用程序是否正在后台播放声音

var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying

如果是这样,请尝试以下操作:

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

关于iOS 允许从其他音乐应用程序返回后播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506869/

相关文章:

c++ - Firebase:可以混合使用 C++ 和 Objective-C SDK 吗?

ios - Swift - 在 UITableViewCell 中添加 buttonTapped 函数

swift - 为什么我不能在 if 子句中使用这个 bool 表达式?

ios - 解析注册方法错误

ios - 在 View Controller 之间传递数据

java - objective-C : extending UIViewController leads to unexpected sense of "self" and "super"

ios - 向父对象窗体弹出消息发送消息

iphone - NSFileManager 监视目录

ios - 在 XCode 中以编程方式执行 segue 后的空白 ViewController

ios - UINavigationBar 标题在更改 viewControllers 时被剪裁