我希望我的背景歌曲在另一个应用程序的音乐正在播放时停止播放。例如苹果音乐或 Spotify。如果您下载了 color switch,您就会明白我的意思。目前我正在使用下面的方法,但我的背景歌曲并没有阻止它与正在播放的音乐混合。(这种方法有点适用于苹果音乐,但不适用于 Spotify)。我希望我的歌曲无限期地播放,直到其他音乐在背景中播放,并在其他音乐暂停时再次播放。(尝试使用颜色切换)
谢谢
func setUpAudio() {
//dead = SKAction.playSoundFileNamed(SoundFile.dead, waitForCompletion: true)
// buttonpress = SKAction.playSoundFileNamed(SoundFile.button, waitForCompletion: true)
if GameScene.backgroundMusicPlayer == nil {
let backgroundMusicURL = Bundle.main.url(forResource: SoundFile.BackgroundMusic, withExtension: nil)
do {
let theme = try AVAudioPlayer(contentsOf: backgroundMusicURL!)
GameScene.backgroundMusicPlayer = theme
} catch {
// couldn't load file :[
}
GameScene.backgroundMusicPlayer.numberOfLoops = -1
}
if !GameScene.backgroundMusicPlayer.isPlaying {
GameScene.backgroundMusicPlayer.play()
}
let audioSession = AVAudioSession.sharedInstance()
/*try!audioSession.setCategory(AVAudioSessionCategoryAmbient, with: AVAudioSessionCategoryOptions.mixWithOthers
)*/
do { try!audioSession.setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true) }
catch let error as NSError { print(error) }
//s audio from other sessions to be ducked (reduced in volume) while audio from this session plays
}
最佳答案
你的 session 类别允许混合,这不是你想要的。尝试使用 AVAudioSessionCategoryPlayback
或 AVAudioSessionCategorySoloAmbient
,因为它们不允许您的应用与其他应用混合音频。
关于ios - 当 Apple Music/Spotify 在后台播放时暂停的 Swift 背景音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737442/