Proper way to pause/resume effects and BGM simple audio engine iOS

Proper way to pause/resume effects and BGM simple audio engine iOS
0
#1

Hi, I would like to ask you how manage pause and resume effects and BGM in background/foreground with Simple Audio Engine (Cocos2dx 3.17)

Now I was working to iOS but also the solution for Android interests me for the near future

In the default code of AppDelegate.cpp od TestCpp I found this:

void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

#if USE_AUDIO_ENGINE
    AudioEngine::pauseAll();
#elif USE_SIMPLE_AUDIO_ENGINE
    SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    SimpleAudioEngine::getInstance()->pauseAllEffects();
#endif
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

#if USE_AUDIO_ENGINE
    AudioEngine::resumeAll();
#elif USE_SIMPLE_AUDIO_ENGINE
    SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    SimpleAudioEngine::getInstance()->resumeAllEffects();
#endif
}

With this code I have a problem with background/foreground
At the first background/foreground audio effects don’t resume (but they are queued (??))
At the second background/foreground they start, but after a while start also those that was queued in the previous “session”

In some post of 2014 someone suggested to comment pause/resume BGM
Infact this solutions works, and iOS automatically pause and resume BGM

I continue to try and I test that I can comment also pause/resume of audio effects (almost on iOS)

So I’m cunfused, what is the proper way to manage effects and BGM during background/foreground in iOS??

Maybe the correct way is different from remove pause/resume of BGM?
Otherwise, I don’t undestand because cocos2d-x continue to use bad code after years and years

Why in the default code there are the pause and resume of effects and BGM, but does the system work even without it?
They are needed for some special cases that I haven’t tested?

I remember that I’m talking about iOS 8 or above

Thank you

0 Likes

#2

Sorry, I dont quite understand what you want to do? Do you have several music files that you want to play consecutively?

0 Likes

#3

Thank you for reply

Mine is a very simple use, I have a BGM and some effects play in the game, by some user events.

with:
Cocos2d-x 3.17
iOS
Simple Audio Engine
AppDelegate Background/Foreground as described above (copied from TestCpp)

  1. When app goes background/foreground first time, effects don’t play at the resume, I mean new effects started by events
  2. If app goes backgroun/foreground another time, effects play (also, very strange, it plays the effects launched at point 1)

If I delete row about pause/resume backgroundMusic in AppDelegate (only backgroundMusic, NOT effects), this strange behaviour desappear

I would like to know what is the proper way to manage background/foreground of BGM and effects with Cocos, beacause the default code in the TestCpp seems not working.

0 Likes

#4

ok, I see now. Thanks for writing more about it. I’ve got a few MacOS issues I have promised others so I will dedicate some time to just these issues.

0 Likes