I am currently using the following statement to detect music:
if MPMusicPlayerController.systemMusicPlayer().playbackState == .Playing {
print("There is music playing")
}
Great, however this will only work for iTunes player, and not music that might be coming from a different app, specifically talking about Spotify.
I don't need to know the song being played, simply whether there is anything playing at all, so I can decide whether I provide my own background music for my game or not.
Edit: ideally the solution should cover any 3rd party music program, not just Spotify.
Given iOS: How do I detect if music is playing in any background music app?
the Swift version would be:
let isOtherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying()
However, the developer docs suggest that starting with iOS 8.0 you should use secondaryAudioShouldBeSilencedHint instead:
if (AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint()) {
print("another application with a non-mixable audio session is playing audio")
}
Related
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.
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)
I'm developing a game app which is having background music. but I don't want to play background music of game if system sound is already playing.Please help me.
All the user has to do is flip the switch on the side of the device (silent mode). Music will keep playing but app sounds will will be disabled.
Phonegap media play method has a magic option playAudioWhenScreenIsLocked = false that does good job:
Prevents app music from playing when app is in background;
Makes app music obey the hardware "mute" button: without this option your app will ignore if user device is muted and play sound anyway.
But there is third hidden magic in this option. It stops current playing song from native Music app. Most of all it's annoying when I want to play short single sound.
Actually I would like to implement this scenario:
Music: Check if system music is playing on the app start. If it is not, play my app music, otherwise let user enjoy his own media.
Sound FX: Just play my little sound once and don't spoil anything.
And never play anything in background and when device is muted! Even if system Music app can let itself do so.
Has anyone managed to do something similar?
Thank you.
Ended up using https://github.com/floatinghotpot/cordova-plugin-nativeaudio (https://build.phonegap.com/plugins/2291)
Be aware: simple sound effects are not working in Simulator.
I have encountered an issue with my ios cocos2d app, that while certain apps are open in the background, my app's music doesn't play.
After I terminate those apps from the background, the music works again.
I use the following mode for CDAudioManager:
[[CDAudioManager sharedManager] setMode:kAMM_FxPlusMusicIfNoOtherAudio];
I use this in order to allow the user to play background music from his music player.
When I change this mode to kAMM_FxPlusMusic for example, my app's music is played even if those apps are open - which is great. However, the music from the music player in this case is paused, which is not what I want.
The main question is:
Why do some apps cause my app's music to not play, and can I do something about it while still allowing the user to play music from his music player?
As you can suggest, I have a trouble =)
In the game I use CocosDenshion for playing both background music and sound effects. As far as I know and can see in the code, it uses AVAudioPlayer to play background music. The trouble is appeared when I try to launch my app with music from iPod library.
I set audio category to AmbientSound (tried setting both with AVAudioSession and C interface). If I then try to play game background track directly using AVAudioPlayer, it plays with iPod music in background without problems. But if try to play background music with SimpleAudioEngine, only iPod music is playing.
Can someone give an advice how to make CocosDenshion work in this case?