Skip to content

Commit 3f7d833

Browse files
committed
fixed audio issues: cocos2d#18342 and cocos2d#17387
1 parent 849e723 commit 3f7d833

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

cocos/audio/AudioEngine.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,34 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
210210
profileHelper = &_audioPathProfileHelperMap[profile->name];
211211
profileHelper->profile = *profile;
212212
}
213+
214+
// if (_audioIDInfoMap.size() >= _maxInstances) {
215+
// log("Fail to play %s cause by limited max instance of AudioEngine",filePath.c_str());
216+
// break;
217+
// }
213218

214219
if (_audioIDInfoMap.size() >= _maxInstances) {
215-
log("Fail to play %s cause by limited max instance of AudioEngine",filePath.c_str());
216-
break;
220+
double oldestTimestamp = std::numeric_limits<double>::max();
221+
int oldestId = INVALID_AUDIO_ID;
222+
std::string ofilePath;
223+
for(auto it = _audioIDInfoMap.begin(); it != _audioIDInfoMap.end(); ++it){
224+
if(it->second.timestamp < oldestTimestamp && !it->second.loop){
225+
oldestId = it->first;
226+
oldestTimestamp = it->second.timestamp;
227+
ofilePath = *it->second.filePath;
228+
}
229+
}
230+
231+
if(oldestId == INVALID_AUDIO_ID){
232+
log("Fail to play %s cause by limited max instance of AudioEngine", filePath.c_str());
233+
break;
234+
}
235+
236+
log("Max instance limit of AudioEngine exceeded. Stopping the oldest sound with id: %d and path: %s", oldestId, ofilePath.c_str());
237+
238+
AudioEngine::stop(oldestId);
217239
}
240+
218241
if (profileHelper)
219242
{
220243
if(profileHelper->profile.maxInstances != 0 && profileHelper->audioIDs.size() >= profileHelper->profile.maxInstances){
@@ -247,6 +270,7 @@ int AudioEngine::play2d(const std::string& filePath, bool loop, float volume, co
247270
audioRef.volume = volume;
248271
audioRef.loop = loop;
249272
audioRef.filePath = &it->first;
273+
audioRef.timestamp = utils::gettime();
250274

251275
if (profileHelper) {
252276
profileHelper->lastPlayTime = utils::gettime();

cocos/audio/android/AudioPlayerProvider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct AudioFileIndicator
7171
static AudioFileIndicator __audioFileIndicator[] = {
7272
{"default", 128000}, // If we could not handle the audio format, return default value, the position should be first.
7373
{".wav", 1024000},
74-
{".ogg", 128000},
74+
{".ogg", 512000}, //128000
7575
{".mp3", 160000}
7676
};
7777

cocos/audio/include/AudioEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ class EXPORT_DLL AudioEngine
335335
bool loop;
336336
float duration;
337337
AudioState state;
338+
double timestamp;
338339

339340
AudioInfo();
340341
~AudioInfo();

tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.cpp

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using namespace cocos2d::experimental;
3232

3333
AudioEngineTests::AudioEngineTests()
3434
{
35+
ADD_TEST_CASE(AudioIssueMaxInstanceTest);
3536
ADD_TEST_CASE(AudioIssue11143Test);
3637
ADD_TEST_CASE(AudioControlTest);
3738
ADD_TEST_CASE(AudioLoadTest);
@@ -651,12 +652,59 @@ std::string LargeAudioFileTest::title() const
651652
return "Test large audio file";
652653
}
653654

654-
bool AudioIssue11143Test::init()
655+
bool AudioIssueMaxInstanceTest::init()
655656
{
656657
if (AudioEngineTestDemo::init())
657658
{
658659
auto& layerSize = this->getContentSize();
660+
661+
auto playItem = TextButton::create("play", [](TextButton* button){
662+
float diff = 0.05f;
663+
664+
AudioEngine::play2d("background.wav", true);
665+
666+
for(int i = 0; i < 200; i++){
667+
float delay = diff * (i + 1);
668+
int j = i % 10 + 81;
669+
670+
char buff1[255];
671+
snprintf(buff1, sizeof(buff1), "k%d", i);
672+
std::string key = buff1;
673+
674+
char buff2[255];
675+
snprintf(buff2, sizeof(buff2), "audio/SoundEffectsFX009/FX0%d.mp3", j);
676+
std::string path = buff2;
677+
678+
button->scheduleOnce([path](float dt){
679+
AudioEngine::play2d(path);
680+
}, delay, key);
681+
}
682+
});
683+
playItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
684+
addChild(playItem);
685+
686+
return true;
687+
}
688+
689+
return false;
690+
}
659691

692+
std::string AudioIssueMaxInstanceTest::title() const
693+
{
694+
return "Max instance exceeded issue test";
695+
}
696+
697+
std::string AudioIssueMaxInstanceTest::subtitle() const
698+
{
699+
return "There shouldn't be any blank break in sound.\nBackground music shouldn't stop.";
700+
}
701+
702+
bool AudioIssue11143Test::init()
703+
{
704+
if (AudioEngineTestDemo::init())
705+
{
706+
auto& layerSize = this->getContentSize();
707+
660708
auto playItem = TextButton::create("play", [](TextButton* button){
661709
AudioEngine::play2d("audio/SoundEffectsFX009/FX082.mp3", true);
662710
AudioEngine::stopAll();
@@ -668,14 +716,14 @@ bool AudioIssue11143Test::init()
668716
AudioEngine::stop(audioId);
669717
AudioEngine::play2d("audio/SoundEffectsFX009/FX083.mp3");
670718
}, 0.3f, key);
671-
719+
672720
});
673721
playItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
674722
addChild(playItem);
675-
723+
676724
return true;
677725
}
678-
726+
679727
return false;
680728
}
681729

tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ class AudioLoadTest : public AudioEngineTestDemo
160160
virtual std::string title() const override;
161161
};
162162

163+
class AudioIssueMaxInstanceTest : public AudioEngineTestDemo
164+
{
165+
public:
166+
CREATE_FUNC(AudioIssueMaxInstanceTest);
167+
168+
virtual bool init() override;
169+
170+
virtual std::string title() const override;
171+
virtual std::string subtitle() const override;
172+
};
173+
163174
class AudioIssue11143Test : public AudioEngineTestDemo
164175
{
165176
public:

0 commit comments

Comments
 (0)