简体   繁体   English

Swift - 检测音乐播放,无论是Spotify还是iTunes

[英]Swift - Detect music playing, whether it's Spotify or iTunes

I am currently using the following statement to detect music: 我目前正在使用以下语句来检测音乐:

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

Great, however this will only work for iTunes player, and not music that might be coming from a different app, specifically talking about Spotify . 很棒,但这只适用于iTunes播放器,而不是可能来自不同应用的音乐,特别是谈论Spotify

I don't need to know the song being played, simply whether there is anything playing at all, so I can decide whether I provide my own background music for my game or not. 我不需要知道正在播放的歌曲,简单地说是否有任何播放,所以我可以决定是否为我的游戏提供自己的背景音乐。

Edit: ideally the solution should cover any 3rd party music program, not just Spotify. 编辑:理想情况下,解决方案应涵盖任何第三方音乐节目,而不仅仅是Spotify。

Given iOS: How do I detect if music is playing in any background music app? 鉴于iOS:如何检测音乐是否在任何背景音乐应用程序中播放?

the Swift version would be: Swift版本将是:

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

However, the developer docs suggest that starting with iOS 8.0 you should use secondaryAudioShouldBeSilencedHint instead: 但是, 开发人员文档建议从iOS 8.0开始,您应该使用secondaryAudioShouldBeSilencedHint

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM