2

I want to allow a player to use their music in my game, so I want to know how to detect if the player is playing music from his music library so I can let his music continue playing and if not let my music play in the background.

I use this code to play music in my GameViewController:

let bgMusicURL: NSURL = NSBundle.mainBundle().URLForResource("Squart-GameMusic", withExtension: "wav")!

do {
    Data.GameMusic = try AVAudioPlayer(contentsOfURL: bgMusicURL, fileTypeHint: nil)
} catch {
    return print("no music file")
}

Data.GameMusic.numberOfLoops = 1
Data.GameMusic.prepareToPlay()
Data.GameMusic.play()

The problem is that when I try to play music from Music Library the sound stops and it lets my app play music instead of the Music Library.

Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
Michel Kansou
  • 2,851
  • 4
  • 13
  • 22

1 Answers1

2

Check whether the music is playing or not in the background like this

if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
    print("Music is playing")
} else {
    print("Play your music")
}

Don't forget to import:

import MediaPlayer

And if you want to play the sound simultaneously then use this code to play the sound file

func playSound {
    var soundID: SystemSoundID = 0
    let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "myFileName", "mp3", nil)
    AudioServicesCreateSystemSoundID(soundURL, &soundID)
    AudioServicesPlaySystemSound(soundID)
} 
Rajat
  • 10,977
  • 3
  • 38
  • 55
  • Thank you it work for music in background but when i use sound effect in my game the background music get cut i use the same code to play sound effect in my game is there another way to play sound effect while the background music is played ? – Michel Kansou Aug 25 '15 at 16:33
  • i am not getting you properly – Rajat Aug 25 '15 at 16:36
  • Do you want to play your music while the app is in background. – Rajat Aug 25 '15 at 16:38
  • sorry for bad explanation i mean my game is about smashing square when the user touche a square it explode and it play a sound effect for 1 second when that sound is played the background music turn off – Michel Kansou Aug 25 '15 at 16:39
  • Register a notification on 'AVAudioPlayer' which will trigger when your sound stops. In the target of that notification put 'MPMusicPlayerController.systemMusicPlayer().play()' which will resume the background music. – Rajat Aug 25 '15 at 16:47
  • does that mean it will play the sound effect and then resume the background music ? – Michel Kansou Aug 25 '15 at 16:48
  • but is there a way to play them at the same time ? – Michel Kansou Aug 25 '15 at 16:50
  • i try it but it play the sound effect 1 time and then i doesn't play it any more all i want is to play systemMusicPlayer music and the sound effect at the same time like in any game for example an fps shooter you have the soundtrack and the sound effect of bullets,fire ... – Michel Kansou Aug 25 '15 at 16:57
  • I get an error 'CFRelease' is unavailable: Core Foundation objects are automatically memory managed – Michel Kansou Aug 25 '15 at 17:00
  • Just remove CFRelease(soundURL) – Rajat Aug 25 '15 at 17:01
  • i got another error Missing return in a function expected to return 'SystemSoundID' – Michel Kansou Aug 25 '15 at 17:04