DJRIFFLE
DJRIFFLE

Reputation: 31

Swift - Playing Audio file and Itunes Music simultaneously

I am currently building a game with swift and spritekit. I want to play a sound in a certain part of the code. Currently I am using 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. 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.

Upvotes: 3

Views: 774

Answers (1)

Craig Siemens
Craig Siemens

Reputation: 13276

You'll need to set your audio session to use a category that allows mixing. By default it uses AVAudioSessionCategorySoloAmbient which is non mixable.

I don't know which category will fit your needs but AVAudioSessionCategoryAmbient allows mixing.

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

Upvotes: 2

Related Questions