0

I'm making a game in Cocos2D iOS and would like play a short audio clip (in .m4a format) when the launch image (Default) is displayed.

I tried adding this line: [[OALSimpleAudio sharedInstance] playBg:@"clip.m4a"];

in application didFinishLaunchingWithOptions but doesn't work.

I also tried playing a .caf file instead of .m4a, but it also doesn't play.

Can you guys please help me in this regard?

Thanks a lot!

Hyder
  • 1,163
  • 2
  • 13
  • 37

1 Answers1

1

Are you talking about the default loading screen that is defined in your project settings as 'default.png'? If yes, I don't think it's possible to play a sound at the same moment the app is launched, because it is currently loading it. What you could do however is a IntroScene that would only implement the onEnter method and immediately apply a transition to, example, your Main Menu. This is what I am doing right now in my game (using cocos2D 2.1, but I guess this isn't very different from a version to another) :

-(void) onEnter
{
    [super onEnter];

    CCScene *scene;

    //Check if app has already launched once
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {
        scene = [MainMenuScene sceneWithParticles:nil];
    }else{
        scene = [Tutorial sceneWithParticle:nil];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    //PLAY YOUR SOUND HERE

    //Transition
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:scene]];
}

Hope this helps! :)

RaphBlanchet
  • 575
  • 6
  • 23
  • OK, but why wouldn't playing the sound in the `didFinishLaunchingWithOptions` not work? – Tibor Udvari Apr 09 '14 at 08:53
  • That's what I don't understand. I looked [here](http://forums.macrumors.com/showthread.php?t=1150624) and tried but it doesn't seem to work. Any help pls? – Hyder Apr 09 '14 at 10:08
  • 1
    I'm no expert in sounds and iPhone, but there's some things you could try : 1)Try playing your sound anywhere else in your app (i.e Main Menu) is it working? 2)Maybe there's a problem when reading a .m4a file. Try converting it to .mp3 and see if it works (Already had a problem with .wav) 3)Maybe your file isn't added to your project even if it's in XCode. Click on your file and in the right pane check 'Target membership > YourGame' 4)Last resort, sometimes work : Make a clean build and try again (Product > Clean)! – RaphBlanchet Apr 09 '14 at 15:49
  • @UnluckyPotato , It worked with a .mp3 file ( `[[OALSimpleAudio sharedInstance] playBg:@"clip.mp3"];` ). Thanks! However, even by placing this code in `didFinishLaunchingWithOptions` , the audio plays in between the transition from the Default.png to the Main Scene. Anyhow, I got it to play atleast. – Hyder Apr 09 '14 at 16:43
  • 1
    Yeah I don't think it's possible to get the sound to play during the default screen, mainly because it is loading your files during that time! And that's why your app only launches when everything is ready! :) Glad I could help! – RaphBlanchet Apr 09 '14 at 17:17