我希望在播放来自另一个应用程序的音乐时停止播放背景音乐。例如苹果音乐或Spotify。如果您下载颜色开关,您将会明白我的意思。目前,我正在使用以下方法,但我的背景歌曲并未停止它与正在播放的音乐混合(此方法有点适用于苹果音乐,但不适用于Spotify)。我希望我的歌曲可以无限期播放,直到其他音乐在后台播放,并且在其他音乐暂停时重新开始播放。(尝试使用Color Switch)

谢谢

 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 类别允许混合,这不是您想要的。尝试使用AVAudioSessionCategoryPlaybackAVAudioSessionCategorySoloAmbient,因为这些不允许您的应用程序将音频与其他应用程序混合。

See the docs for more info

关于ios - 苹果背景音乐/Spotify在后台播放时暂停的快速背景音乐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44737442/

10-14 20:27