简体   繁体   English

Swift - 同时播放音频文件和Itunes Music

[英]Swift - Playing Audio file and Itunes Music simultaneously

I am currently building a game with swift and spritekit. 我目前正在使用swift和spritekit构建游戏。 I want to play a sound in a certain part of the code. 我想在代码的某个部分播放声音。 Currently I am using AVAudioPlayer. 目前我正在使用AVAudioPlayer。 I am currently using this method. 我目前正在使用这种方法。

player = try AVAudioPlayer(contentsOfURL: url)
player.prepareToPlay()
player.play()

The sound plays correctly, but if I were to be listening to music through itunes while the sound is played my itunes music is stopped and the sound is played. 声音播放正确,但如果我在播放声音的同时通过iTunes听音乐,我的音乐会停止并播放声音。 I have played other people's games in the past where ingame sound effects play and do not effect music that was already playing from another app. 我过去玩过其他人的游戏,其中游戏声音效果发挥并且不影响已经从另一个应用播放的音乐。 Can somone explain how this is achieved.Thanks a bunch. 可以解释一下这是如何实现的。谢谢你。

You'll need to set your audio session to use a category that allows mixing. 您需要将音频会话设置为使用允许混音的类别。 By default it uses AVAudioSessionCategorySoloAmbient which is non mixable. 默认情况下,它使用不可混合的AVAudioSessionCategorySoloAmbient

I don't know which category will fit your needs but AVAudioSessionCategoryAmbient allows mixing. 我不知道哪个类别适合您的需求,但AVAudioSessionCategoryAmbient允许混音。

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

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

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