How do I set things up so that SimpleAudioEngine's playEffect plays along with the ipod music rather than turning it off?
SimpleAudioEngine's playEffect turns off ipod music.
(8 posts) (6 voices)-
Posted 10 months ago #
-
You need to set the correct play mode based on what you're doing:
/** Different modes of the engine */ typedef enum { kAMM_FxOnly, //!Other apps will be able to play audio kAMM_FxPlusMusic, //!Only this app will play audio kAMM_FxPlusMusicIfNoOtherAudio, //!If another app is playing audio at start up then allow it to continue and don't play music kAMM_MediaPlayback, //!This app takes over audio e.g music player app kAMM_PlayAndRecord //!App takes over audio and has input and output } tAudioManagerMode;Posted 10 months ago # -
Where do you set this? In which method? An example would be nice! :)
Posted 10 months ago # -
You'd do something like this when you set things up.
[CDAudioManager initAsynchronously: kAMM_FxOnly]; or [CDAudioManager configure: kAMM_FxOnly]; or [[CDAudioManager sharedManager] setMode: kAMM_FxOnly];You'd probably use the first example. Do that before you play any sounds.
The value you pass depends on what you're trying to do, but try the different enums above and see which behaves the way you want.
If you look at the GameSoundManager in the Tom the Turret sample, you'll see an example.
Posted 10 months ago # -
Hi @clarus or anyone who can help.
I've got a similar problem. I'm trying to let people use their iPod music if it's already playing.
The option kAMM_FxPlusMusicIfNoOtherAudio works great if they are playing music before they ever play the game.
However there's a problem with multi-tasking. If someone runs my app, doesn't like the music so presses the home key and finds their iPod music then runs it again my app will shutdown their iPod music. AVAudioSessionCategorySoloAmbient is still set.
I've tried running the CDAudioManager configure in
- (void)applicationDidBecomeActive:(UIApplication *)application { [CDAudioManager configure: kAMM_FxPlusMusicIfNoOtherAudio]; [[CCDirector sharedDirector] resume]; }I've also tried
[[CDAudioManager sharedManager] setMode: kAMM_FxPlusMusicIfNoOtherAudio];and
[CDAudioManager initAsynchronously: kAMM_FxPlusMusicIfNoOtherAudio];But nothing seems to have the desired effect of switching from AVAudioSessionCategorySoloAmbient to AVAudioSessionCategoryAmbient (ie. letting iPod music play after an app becomes active again.
Any ideas on how this can be done? I've searched through the forums and no one seems to be mentioning it, but it seems a pretty big flaw for anyone who want to give people the option of playing their own music.
Thanks,
TravisPosted 6 months ago # -
Hi,
Not sure if this is the best way to do it but I came up with my own solution.
I set the default settings in CDAudioManager to be
_audioSessionCategory = AVAudioSessionCategoryAmbient; willPlayBackgroundMusic = YES;regardless of whether there's other music playing.
Note that normally when you choose AVAudioSessionCategoryAmbient the willPlayBackgroundMusic variable is set to NO. That's so that two songs aren't playing over the top of each other. So to avoid that I do a test for whether isOtherAudioPlaying whenever I want to play background music. Either after my app enters the background or on startup etc.
So in my app delegate I've added the following
- (void)applicationWillResignActive:(UIApplication *)application { [[CCDirector sharedDirector] pause]; [[SimpleAudioEngine sharedEngine] stopBackgroundMusic]; } -(void) applicationWillEnterForeground:(UIApplication*)application { [[CCDirector sharedDirector] startAnimation]; [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"MyMusic.caf"];//play background music if (([[[NSUserDefaults standardUserDefaults] objectForKey:@"Sound"] boolValue] == NO) || ([[CDAudioManager sharedManager] isOtherAudioPlaying])) { [[SimpleAudioEngine sharedEngine] stopBackgroundMusic]; } }I've got a button that turns sound on or off which sets my NSUserDefaults Sound Object. So anywhere I test if sound is on I also have to add the [[CDAudioManager sharedManager] isOtherAudioPlaying] test.
Might not be the most elegant solution but it works.
Posted 6 months ago # -
However there's a problem with multi-tasking. If someone runs my app, doesn't like the music so presses the home key and finds their iPod music then runs it again my app will shutdown their iPod music. AVAudioSessionCategorySoloAmbient is still set.
Did you have a resign handler set up as is illustrated in the FadeToGrey example? You should see that example handles this use case.
Posted 6 months ago # -
I have run into the same issue now, thanks steve for pointing me to the solution, the magic line of code is:
[[CDAudioManager sharedManager] setResignBehavior:kAMRBStopPlay autoHandle:YES];Personally I'd argue for this being the default for kAMM_FxPlusMusicIfNoOtherAudio in todays multitasking ios environment...
Posted 4 months ago #
Reply
You must log in to post.