0
votes

I am optimizing a game so that when there is an incoming call or if it is moved to the background for any other reason, it should stop the music, timers and pause correctly.

It works great other than the fact that for some reason the music doesn't stop playing even though I issued a command for it to stop. What's weirder is that when the game returns to the foreground there's 2 background musics playing instead of one.

Here is my some of my code, nothing too complicated or out of the ordinary:

  ...

NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
            NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate);
            NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate);
...
private function handleActivate(event:Event):void
        {

            stage.frameRate = previousFrameRate;

//if the music was on when the game is moved to the background, then replay it when its moved back to the foreground
            if(musicOn) 
            {
                BGMusic.play();
            }


        }

        private function handleDeactivate(event:Event):void
        {
            BGMusic.stop();
            stage.frameRate = 0;
        }

It is worth noting that if I don't replay the music when then game returns to the foreground (i.e. when I don't use BGMuusic.play() in handleActivate), there won't be any music as expected. It is only when I stop the music AND resume it later when moving to the foreground that the background music doesn't stop and I get two playing tracks.

Any ideas? As I said, everything else works fine, and the game correctly pauses. I am testing this on Flash builder Emulator since I don't have the necessarily certificates to test it directly on the iphone. So maybe it is a problem with the emulator itself rather than the code.

Thanks in advance

EDIT: Am writing this with flash and adobe air

2

2 Answers

1
votes

Well that was stupid of me. Everything is working fine I just forgot to add another condition to the if statement. So instead of:

            if(musicOn) 
            {
                BGMusic.play();
            }

It should be

            if(musicOn && !BGMusic.isPlaying())
            {
                BGMusic.play();
            }

Everything works fine now...I will change the variable name of musicOn to something clearer like musicButtonOn.

Anyways thanks for help. If you have any tips or comments feel free to tell me =D

0
votes
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
      NSLog(@"Playing stoped");
      [player stop];
}