cocos2d-x icon indicating copy to clipboard operation
cocos2d-x copied to clipboard

iOS AudioEngine stops looping audio

Open Sleicreider opened this issue 6 years ago • 8 comments

  • cocos2d-x version: 3.17.1
  • devices test on: iPad Mini 2, iPhone 6
  • developing environments
    • Xcode version: Version 10.1 (10B61)

Steps to Reproduce:

Audio suddenly stops, or doesn't resume when entering the running app from the homescreen or after hitting a breakpoint. (happens more often on the ipad mini 2 and in real project where much more is going on)

Tried to create looping background music.

Tested with cpptest NewAudioEngineTest - 8. Test large audio file

  1. Open the cocos2d-x cpptest project NewAudioEngineTest.cpp

  2. Change LargeAudioFileTest::init() AudioEngine::play2d("audio/LuckyDay.mp3") to AudioEngine::play2d("audio/LuckyDay.mp3", true)

  3. Run the app on a ios device and play the music from NewAudioEngine 8. Test large audio file

  4. Either break in the debugger and resume (should be 100%) or try to tab out of the game and back in again.

  5. Music shouldn't be playing anymore. (but you can start it again).

Reproducing it by tabing out of the game and back in, happens more likely when you have mulltiple apps open and running, not sure if this could be memory related.

Additionaly from my observation and debugging so far:


AudioEngineImpl-inl.mm AudioEngineImpl::update(..)

alGetSourcei(alSource, AL_SOURCE_STATE, &sourceState);

sets the sourceState to AL_STOPPED

which causes to enter the if conditional else if (player->_ready && sourceState == AL_STOPPED)

where it gets removed from the audio map. maybe the openal audio streaming gets interrupted?

For more check out my forum post https://discuss.cocos2d-x.org/t/audio-stopping-on-ios/45887/6

Sleicreider avatar Mar 08 '19 21:03 Sleicreider

Hi,

I was wondering if there has been an update on this issue? Is there an eta for when this will be looked into?

riwells avatar Apr 12 '19 11:04 riwells

Maybe I'll find time and give it another shot and look into it again in the next few weeks, since my iOS release is somewhat dependent on it

Sleicreider avatar Apr 12 '19 15:04 Sleicreider

I'm currently taking a look at this again, if anyone as any updates on this please let me know: Not sure if this is somehow related to this issues: https://github.com/cocos2d/cocos2d-x/issues/10724

Currently this seems to be only an issue on iOS, on win32 and osx it never gets to the else if (player->_ready && sourceState == AL_STOPPED) conditional. Not 100% sure how its implemented on Android to be able to test it at the moment since it's implemented somehow on java i guess.

Sleicreider avatar May 20 '19 11:05 Sleicreider

Update: After some digging I could find some relation to the rotateBufferThread(..) which gets called and probably causes somehow the AL_STOPPED state.

https://github.com/cocos2d/cocos2d-x/issues/17800 https://github.com/cocos2d/cocos2d-x/issues/18597

Especially the last one seems to be important here. Those are similar crashes, maybe even the exact same.

I'm not sure what the final best solution was to this, for me it seems like the solution was to increase the PCMDATA_CACHEMAXSIZE value to fit your looping music file, not 100% if it still sometimes crashes, but it definitely causes more memory usage, since its not streamed anymore?.

@drelaptop It seems like you provided a solution to the last issue link. Could my issue be related to this?

Sleicreider avatar May 20 '19 14:05 Sleicreider

A quick fix is to double the data on the buffer to 200ms

Used this quick fix so far it worked properly. I think rotateBufferThread somehow interrupted by UI thread, an easy way to reproduce this problem is run the app on iPhone 5S then slide out the Control Center, older device is more easily to reproduce.

qoozta avatar Jul 10 '19 01:07 qoozta

Same issue can reproduce on:

  • iphone SE with iOS 13.1.3
  • cocos2d-x 3.17.2

But increase PCMDATA_CACHEMAXSIZE value to 10485760, this problem is gone.

boyjunqiang avatar Oct 25 '19 02:10 boyjunqiang

Same issue can reproduce on:

  • iphone SE with iOS 13.1.3
  • cocos2d-x 3.17.2

But increase PCMDATA_CACHEMAXSIZE value to 10485760, this problem is gone.

goooooooood

老铁这方法可以,引擎组的可以看看

zoxin avatar May 15 '20 10:05 zoxin

I increase the PCMDATA_CACHEMAXSIZE value to 20485760 and this problem is gone ... I use cocos2dx 4.0 ...

atship avatar Aug 31 '21 01:08 atship