【问题标题】:Swift - Detect music playing, whether it's Spotify or iTunesSwift - 检测音乐播放,无论是 Spotify 还是 iTunes
【发布时间】:2016-11-18 00:51:14
【问题描述】:

我目前正在使用以下语句来检测音乐:

if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
    print("There is music playing")
}

很好,但这仅适用于 iTunes 播放器,不适用于可能来自其他应用的音乐,特别是关于 Spotify

我不需要知道正在播放的歌曲,只需要知道是否有任何东西在播放,因此我可以决定是否为我的游戏提供自己的背景音乐。

编辑:理想情况下,该解决方案应涵盖任何第三方音乐节目,而不仅仅是 Spotify。

【问题讨论】:

    标签: ios swift spotify audio-player background-music


    【解决方案1】:

    给定iOS: How do I detect if music is playing in any background music app?

    Swift 版本是:

    let isOtherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying()
    

    但是,developer docs 建议从 iOS 8.0 开始,您应该改用 secondaryAudioShouldBeSilencedHint

    if (AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint()) {
       print("another application with a non-mixable audio session is playing audio")
    }
    

    【讨论】:

    • 我们能得到 AVAudioPlayer 正在播放的内容吗?或者也许获取数据?或任何其他相关信息?
    猜你喜欢
    • 2011-01-17
    • 2017-05-13
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多