I’m writing a cross platform app (iOS and Android) using the 2.0.3 cocos2dx.
I’m using SimpleAudioEngine to play background music and sound effects.
In AppDelegate.cpp - applicationDidEnterBackground I pause
In AppDelegate.cpp - applicationWillEnterForeground I resume
In my destructor I stop all music and effects
I scheduled a function call to check if the current track has finished playing and start the next random one.
In short the code in playmusic checks if the track is playing using
If not playing then I start a new track
This is working ok.
Closing my app stops the music, starting the scene starts a new random track and when 1 track finishes, the next random one starts.
Pressing the home button, stops the music, reopening the app resumes the music and app correctly
I do have a problem (on Android at least) when I lock the phone (press power button) and unlock it again (press power button again).
The function applicationWillEnterForeground is called even though the phone is still on the lock screen.
This means that the music starts again (and I think the game itself) before I have unlocked the phone.
After unlocking the phone, the applicationDidEnterBackground and applicationWillEnterForeground are both called again.
My playmusic function is called again and isBackgroundMusicPlaying = false so I start a new track even though the old track is still playing.
Is there a way to make sure the app only starts when the phone is actually unlocked (i.e. the app is visible).
Also, is there a way to tell the difference between paused and finished the track?
In both cases isBackgroundMusicPlaying = false.