PuzzleSDK
NewAudioEngineTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2014-2016 Chukong Technologies Inc.
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "platform/CCPlatformConfig.h"
27#include "NewAudioEngineTest.h"
28#include "ui/CocosGUI.h"
29
30using namespace cocos2d;
31using namespace cocos2d::ui;
32
33AudioEngineTests::AudioEngineTests()
34{
51
54
55 //FIXME: Please keep AudioSwitchStateTest to the last position since this test case doesn't work well on each platforms.
57}
58
59namespace {
60
61 class TextButton : public cocos2d::Label
62 {
63 public:
64
65 static TextButton *create(const std::string& text, const std::function<void(TextButton*)> &onTriggered)
66 {
67 auto ret = new (std::nothrow) TextButton();
68
69 TTFConfig ttfconfig("fonts/arial.ttf",25);
70 if (ret && ret->setTTFConfig(ttfconfig)) {
71 ret->setString(text);
72 ret->_onTriggered = onTriggered;
73
74 ret->autorelease();
75
76 return ret;
77 }
78
79 delete ret;
80 return nullptr;
81 }
82
83 void setEnabled(bool enabled)
84 {
85 _enabled = enabled;
86 if(_enabled){
87 this->setColor(Color3B::WHITE);
88 }
89 else {
90 this->setColor(Color3B::GRAY);
91 }
92 }
93
94 private:
95 TextButton()
96 : _onTriggered(nullptr)
97 , _enabled(true)
98 {
99 auto listener = EventListenerTouchOneByOne::create();
100 listener->setSwallowTouches(true);
101
102 listener->onTouchBegan = CC_CALLBACK_2(TextButton::onTouchBegan, this);
103 listener->onTouchEnded = CC_CALLBACK_2(TextButton::onTouchEnded, this);
104 listener->onTouchCancelled = CC_CALLBACK_2(TextButton::onTouchCancelled, this);
105
106 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
107
108 }
109
110 bool touchHits(Touch *touch)
111 {
112 auto hitPos = this->convertToNodeSpace(touch->getLocation());
113 if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width && hitPos.y <= _contentSize.height) {
114 return true;
115 }
116 return false;
117 }
118
119 bool onTouchBegan(Touch *touch, Event *event)
120 {
121 auto hits = touchHits(touch);
122 if (hits){
123 scaleButtonTo(0.95f);
124 }
125 return hits;
126 }
127
128 void onTouchEnded(Touch *touch, Event *event)
129 {
130 if(_enabled) {
131 auto hits = touchHits(touch);
132 if (hits && _onTriggered){
133 _onTriggered(this);
134 }
135 }
136
137 scaleButtonTo(1);
138 }
139
140 void onTouchCancelled(Touch *touch, Event *event)
141 {
142 scaleButtonTo(1);
143 }
144
145 void scaleButtonTo(float scale)
146 {
147 auto action = ScaleTo::create(0.05f, scale);
148 action->setTag(10000);
149 stopActionByTag(10000);
150 runAction(action);
151 }
152
153 std::function<void(TextButton*)> _onTriggered;
154
155 bool _enabled;
156 };
157
158 class SliderEx : public Slider
159 {
160 public:
161 static SliderEx* create(){
162 auto ret = new (std::nothrow) SliderEx();
163 if (ret && ret->init())
164 {
165 ret->loadBarTexture("cocosui/sliderTrack.png");
166 ret->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
167 ret->loadProgressBarTexture("cocosui/sliderProgress.png");
168 ret->setTouchEnabled(true);
169
170 ret->autorelease();
171
172 return ret;
173 }
174 CC_SAFE_DELETE(ret);
175 return ret;
176 }
177
178 void setRatio(float ratio) {
179 ratio = clampf(ratio, 0.0f, 1.0f);
180
181 _ratio = ratio;
182 setPercent(100 * _ratio);
183 }
184
185 float getRatio () {
186 _ratio = 1.0f * _percent / _maxPercent;
187 return _ratio;
188 }
189
190 private:
191 float _ratio;
192 };
193}
194
196: _isDestroyed(std::make_shared<bool>(false))
197{
198}
199
201{
202 *_isDestroyed = true;
203 AudioEngine::uncacheAll();
204 TestCase::onExit();
205}
206
207std::string AudioEngineTestDemo::title() const
208{
209 return "New Audio Engine Test";
210}
211
212void AudioEngineTestDemo::onBackCallback(cocos2d::Ref* sender)
213{
214 AudioEngine::end();
216}
217
218// AudioControlTest
220{
221 auto ret = AudioEngineTestDemo::init();
222 _audioID = AudioEngine::INVALID_AUDIO_ID;
223 _loopEnabled = false;
224 _volume = 1.0f;
225 _duration = AudioEngine::TIME_UNKNOWN;
226 _timeRatio = 0.0f;
227 _updateTimeSlider = true;
228 _isStopped = false;
229
230 std::string fontFilePath = "fonts/arial.ttf";
231
232 auto& layerSize = this->getContentSize();
233
234 _playOverLabel = Label::createWithSystemFont("Play Over", "", 30);
235 _playOverLabel->setPosition(Vec2(layerSize/2) + Vec2(0, 30));
236 _playOverLabel->setVisible(false);
237 addChild(_playOverLabel, 99999);
238
239 auto playItem = TextButton::create("play", [&](TextButton* button){
240 if (_audioID == AudioEngine::INVALID_AUDIO_ID) {
241 _audioID = AudioEngine::play2d("background.mp3", _loopEnabled, _volume);
242
243 if(_audioID != AudioEngine::INVALID_AUDIO_ID) {
244 _isStopped = false;
245
246 button->setEnabled(false);
247 AudioEngine::setFinishCallback(_audioID, [&](int id, const std::string& filePath){
248 log("_audioID(%d), _isStopped:(%d), played over!!!", _audioID, _isStopped);
249
250 _playOverLabel->setVisible(true);
251
252 scheduleOnce([&](float dt){
253 _playOverLabel->setVisible(false);
254 }, 2.0f, "hide_play_over_label");
255
256 assert(!_isStopped); // Stop audio should not trigger finished callback
257 _audioID = AudioEngine::INVALID_AUDIO_ID;
258 ((TextButton*)_playItem)->setEnabled(true);
259
260 _timeRatio = 0.0f;
261 ((SliderEx*)_timeSlider)->setRatio(_timeRatio);
262 });
263 }
264 }
265 });
266 _playItem = playItem;
267 playItem->setPosition(layerSize.width * 0.3f,layerSize.height * 0.8f);
268 addChild(playItem);
269
270 auto stopItem = TextButton::create("stop", [&](TextButton* button){
271 if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
272 _isStopped = true;
273 AudioEngine::stop(_audioID);
274
275 _audioID = AudioEngine::INVALID_AUDIO_ID;
276 ((TextButton*)_playItem)->setEnabled(true);
277 }
278 });
279 stopItem->setPosition(layerSize.width * 0.7f,layerSize.height * 0.8f);
280 addChild(stopItem);
281
282 auto pauseItem = TextButton::create("pause", [&](TextButton* button){
283 if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
284 AudioEngine::pause(_audioID);
285 }
286 });
287 pauseItem->setPosition(layerSize.width * 0.3f,layerSize.height * 0.7f);
288 addChild(pauseItem);
289
290 auto resumeItem = TextButton::create("resume", [&](TextButton* button){
291 if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
292 AudioEngine::resume(_audioID);
293 }
294 });
295 resumeItem->setPosition(layerSize.width * 0.7f,layerSize.height * 0.7f);
296 addChild(resumeItem);
297
298 auto loopItem = TextButton::create("enable-loop", [&](TextButton* button){
300
301 if (_audioID != AudioEngine::INVALID_AUDIO_ID) {
302 AudioEngine::setLoop(_audioID, _loopEnabled);
303 }
304 if (_loopEnabled){
305 button->setString("disable-loop");
306 }
307 else {
308 button->setString("enable-loop");
309 }
310 });
311 loopItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
312 addChild(loopItem);
313
314 auto volumeSlider = SliderEx::create();
315 volumeSlider->setPercent(100);
316 volumeSlider->addEventListener([&](Ref* sender, Slider::EventType event){
317 SliderEx *slider = dynamic_cast<SliderEx *>(sender);
318 _volume = slider->getRatio();
319 if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
320 AudioEngine::setVolume(_audioID, _volume);
321 }
322 });
323 volumeSlider->setPosition(Vec2(layerSize.width * 0.5f,layerSize.height * 0.35f));
324 addChild(volumeSlider);
325
326 auto timeSlider = SliderEx::create();
327 timeSlider->addEventListener([&](Ref* sender, Slider::EventType event){
328 SliderEx *slider = dynamic_cast<SliderEx *>(sender);
329 switch(event){
330 case Slider::EventType::ON_SLIDEBALL_DOWN:
331 _updateTimeSlider = false;
332 break;
333 case Slider::EventType::ON_SLIDEBALL_UP:
334 if (_audioID != AudioEngine::INVALID_AUDIO_ID && _duration != AudioEngine::TIME_UNKNOWN) {
335 float ratio = (float)slider->getPercent() / 100;
336 ratio = clampf(ratio, 0.0f, 1.0f);
337 AudioEngine::setCurrentTime(_audioID, _duration * ratio);
338 }
339 case Slider::EventType::ON_SLIDEBALL_CANCEL:
340 _updateTimeSlider = true;
341 case Slider::EventType::ON_PERCENTAGE_CHANGED:
342 default:
343 //ignore
344 break;
345 }
346 });
347 timeSlider->setPosition(Vec2(layerSize.width * 0.5f,layerSize.height * 0.25f));
348 addChild(timeSlider);
349 _timeSlider = timeSlider;
350
351 auto& volumeSliderPos = volumeSlider->getPosition();
352 auto& sliderSize = volumeSlider->getContentSize();
353 auto volumeLabel = Label::createWithTTF("volume: ", fontFilePath, 20);
354 volumeLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
355 volumeLabel->setPosition(volumeSliderPos.x - sliderSize.width / 2, volumeSliderPos.y);
356 addChild(volumeLabel);
357
358 auto& timeSliderPos = timeSlider->getPosition();
359 auto timeLabel = Label::createWithTTF("time: ", fontFilePath, 20);
360 timeLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
361 timeLabel->setPosition(timeSliderPos.x - sliderSize.width / 2, timeSliderPos.y);
362 addChild(timeLabel);
363
364 this->schedule(CC_CALLBACK_1(AudioControlTest::update, this), 0.1f, "update_key");
365
366 return ret;
367}
368
370{
371 if (_audioID != AudioEngine::INVALID_AUDIO_ID ) {
372 if(_duration == AudioEngine::TIME_UNKNOWN){
373 _duration = AudioEngine::getDuration(_audioID);
374 }
375 if(_duration != AudioEngine::TIME_UNKNOWN){
376 auto time = AudioEngine::getCurrentTime(_audioID);
377 _timeRatio = time / _duration;
379 ((SliderEx*)_timeSlider)->setRatio(_timeRatio);
380 }
381 }
382 }
383}
384
386{
387}
388
389std::string AudioControlTest::title() const
390{
391 return "Audio control test";
392}
393
395{
396 if (AudioEngineTestDemo::init())
397 {
398 auto& layerSize = this->getContentSize();
399
400 auto stateLabel = Label::createWithTTF("status:", "fonts/arial.ttf", 30);
401 stateLabel->setPosition(layerSize.width / 2, layerSize.height * 0.7f);
402 addChild(stateLabel);
403
404 auto preloadItem = TextButton::create("preload", [&, stateLabel](TextButton* button){
405 stateLabel->setString("status:loading...");
406 auto isDestroyed = _isDestroyed;
407 AudioEngine::preload("audio/SoundEffectsFX009/FX082.mp3", [isDestroyed, stateLabel](bool isSuccess){
408 if (*isDestroyed)
409 {
410 CCLOG("AudioLoadTest scene was destroyed, no need to set the label text.");
411 return;
412 }
413
414 if (isSuccess)
415 {
416 stateLabel->setString("status:load success");
417 }
418 else
419 {
420 stateLabel->setString("status:load fail");
421 }
422 });
423 });
424 preloadItem->setPosition(layerSize.width * 0.35f, layerSize.height * 0.5f);
425 addChild(preloadItem);
426
427 auto uncacheItem = TextButton::create("uncache", [&, stateLabel](TextButton* button){
428 stateLabel->setString("status:uncache");
429 AudioEngine::uncache("audio/SoundEffectsFX009/FX082.mp3");
430 });
431 uncacheItem->setPosition(layerSize.width * 0.65f, layerSize.height * 0.5f);
432 addChild(uncacheItem);
433
434 return true;
435 }
436
437 return false;
438}
439
440std::string AudioLoadTest::title() const
441{
442 return "Audio preload/uncache test";
443}
444
445
446// PlaySimultaneouslyTest
448{
449 auto ret = AudioEngineTestDemo::init();
450
451 char text[36];
452 int tmp = 81;
453 for(int index = 0; index < TEST_COUNT; ++index){
454 sprintf(text,"audio/SoundEffectsFX009/FX0%d.mp3",tmp + index);
455 _files[index] = text;
456 }
457 _playingcount = 0;
458
459 auto playItem = TextButton::create("play-simultaneously", [&](TextButton* button){
460 int audioId;
461 _playingcount = 0;
462 button->setEnabled(false);
463 auto startTime = utils::gettime();
464 for(int index = 0; index < TEST_COUNT; ++index){
465 audioId = AudioEngine::play2d(_files[index]);
466 if(audioId != AudioEngine::INVALID_AUDIO_ID){
467 _playingcount += 1;
468
469 AudioEngine::setFinishCallback(audioId, [&](int id, const std::string& filePath){
470 _playingcount -= 1;
471 if(_playingcount <= 0){
472 ((TextButton*)_playItem)->setEnabled(true);
473 }
474 });
475 }
476 else {
477 log("%s,%d,Fail to play file:%s",__FILE__,__LINE__ ,_files[index].c_str());
478 }
479 }
480 log("diff time:%lf",utils::gettime() - startTime);
481 });
482 playItem->setPositionNormalized(Vec2(0.5f,0.5f));
483 this->addChild(playItem);
484 _playItem = playItem;
485
486 return ret;
487}
488
490{
491}
492
494{
495 return "Simultaneously play multiple audio";
496}
497
498// AudioProfileTest
500{
501 auto ret = AudioEngineTestDemo::init();
502
503 char text[30];
504 _files[0] = "background.mp3";
505#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
506 _files[1] = "background.caf";
507#else
508 _files[1] = "background.ogg";
509#endif
510
511 std::string fontFilePath = "fonts/arial.ttf";
512 _minDelay = 1.0f;
513 _time = 0.0f;
514
515 _audioProfile.name = "AudioProfileTest";
516 _audioProfile.maxInstances = 3;
517 _audioProfile.minDelay = 1.0;
518
519 Vec2 pos(0.5f,0.7f);
520 for(int index = 0; index < FILE_COUNT; ++index){
521 sprintf(text,"play %s",_files[index].c_str());
522
523 auto playItem = TextButton::create(text, [&](TextButton* button){
524 int index = button->getTag();
525 auto id = AudioEngine::play2d(_files[index], false, 1.0f, &_audioProfile);
526 if(id != AudioEngine::INVALID_AUDIO_ID){
528 _audioCount += 1;
529 char show[30];
530 sprintf(show,"audio count:%d",_audioCount);
531 _showLabel->setString(show);
532
533 AudioEngine::setFinishCallback(id, [&](int id, const std::string& filePath){
534 _audioCount -= 1;
535 char show[30];
536 sprintf(show,"audio count:%d",_audioCount);
537 _showLabel->setString(show);
538 });
539 }
540
541 });
542 playItem->setTag(index);
543 playItem->setPositionNormalized(pos);
544 this->addChild(playItem);
545 pos.y -= 0.15f;
546
547 }
548
549 Vec2 origin = Director::getInstance()->getVisibleOrigin();
550 Size size = Director::getInstance()->getVisibleSize();
551
552 auto profileInfoLabel = Label::createWithTTF("AudioProfile Info:\n max instance:3 \n minimum delay:1.0", fontFilePath, 12);
553 profileInfoLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
554 profileInfoLabel->setPosition(Vec2(origin.x, origin.y + size.height * 0.65f));
555 addChild(profileInfoLabel);
556
557 _audioCount = 0;
558 _showLabel = Label::createWithTTF("audio count:0", fontFilePath, 12);
559 _showLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
560 _showLabel->setPosition(Vec2(origin.x, origin.y + size.height * 0.5f));
561 addChild(_showLabel);
562
563 auto timeSlider = SliderEx::create();
564 timeSlider->setEnabled(false);
565 timeSlider->setPositionNormalized(pos);
566 addChild(timeSlider);
567 _timeSlider = timeSlider;
568
569 this->schedule(CC_CALLBACK_1(AudioProfileTest::update, this), 0.05f, "update_key");
570
571 return ret;
572}
573
575{
576 if(_time > 0.0f)
577 {
578 _time -= dt;
579 ((SliderEx*)_timeSlider)->setRatio(_time / _minDelay);
580 }
581}
582
584{
585}
586
587std::string AudioProfileTest::title() const
588{
589 return "AudioProfileTest";
590}
591
592std::string AudioProfileTest::subtitle() const
593{
594 return "See the console.";
595}
596
597// InvalidAudioFileTest
599{
600 auto ret = AudioEngineTestDemo::init();
601
602 auto playItem = TextButton::create("play unsupported media type", [&](TextButton* button){
603#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
604 AudioEngine::play2d("background.ogg");
605#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
606 AudioEngine::play2d("background.caf");
607#endif
608 });
609 playItem->setPositionNormalized(Vec2(0.5f, 0.6f));
610 this->addChild(playItem);
611
612 auto playItem2 = TextButton::create("play not-existent file", [&](TextButton* button){
613 AudioEngine::play2d("not-existent file.mp3");
614 });
615 playItem2->setPositionNormalized(Vec2(0.5f, 0.4f));
616 this->addChild(playItem2);
617
618 return ret;
619}
620
622{
623}
624
626{
627 return "Test invalid audio file";
628}
629
631{
632 return "Not crash,please see the console.";
633}
634
635// LargeAudioFileTest
637{
638 auto ret = AudioEngineTestDemo::init();
639
640 auto playItem = TextButton::create("play large audio file", [&](TextButton* button){
641 AudioEngine::play2d("audio/LuckyDay.mp3");
642 });
643 playItem->setPositionNormalized(Vec2::ANCHOR_MIDDLE);
644 this->addChild(playItem);
645
646 return ret;
647}
648
650{
651}
652
653std::string LargeAudioFileTest::title() const
654{
655 return "Test large audio file";
656}
657
659{
660 if (AudioEngineTestDemo::init())
661 {
662 auto& layerSize = this->getContentSize();
663
664 //test case for https://github.com/cocos2d/cocos2d-x/issues/18597
665 this->schedule([=](float dt)
666 {
667 CCLOG("issues 18597 audio crash test");
668 for (int i = 0; i< 2;++i)
669 {
670 auto id = AudioEngine::play2d("audio/MUS_BGM_Battle_Round1_v1.caf", true, 1.0f);
671 this->runAction(Sequence::create(
672 DelayTime::create(8.0f),
673 CallFunc::create([=]()
674 {
675 AudioEngine::stop(id);
676 }),
677 nullptr
678 ));
679 }
680 }, 2.0, 10000, 0.0, "audio test");
681 // add label to show the side effect of "UnqueueBuffers Before alSourceStop"
682 _time = 0.0;
683 auto labelTime = Label::createWithBMFont("fonts/bitmapFontTest2.fnt", "time: ");
684 labelTime->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
685 labelTime->setTag(999);
686 this->addChild(labelTime);
687 // update label quickly
688 this->schedule([=](float dt){
689 _time += dt;
690 char timeString[20] = {0};
691 sprintf(timeString, "Time %2.2f", _time);
692 dynamic_cast<Label *>(this->getChildByTag(999))->setString(timeString);
693 }, 0.05, 1000000, 0, "update label quickly");
694
695 return true;
696 }
697
698 return false;
699}
700
701std::string AudioIssue18597Test::title() const
702{
703 return "Test for issue 18597";
704}
705
707{
708 return "no crash for more than 10 minutes";
709}
710
712{
713 if (AudioEngineTestDemo::init())
714 {
715 auto& layerSize = this->getContentSize();
716
717 auto playItem = TextButton::create("play", [](TextButton* button){
718 AudioEngine::play2d("audio/SoundEffectsFX009/FX082.mp3", true);
719 AudioEngine::stopAll();
720
721 auto audioId = AudioEngine::play2d("audio/SoundEffectsFX009/FX082.mp3", true);
722 char key[100] = {0};
723 sprintf(key, "play another sound %d", audioId);
724 button->scheduleOnce([audioId](float dt){
725 AudioEngine::stop(audioId);
726 AudioEngine::play2d("audio/SoundEffectsFX009/FX083.mp3");
727 }, 0.3f, key);
728
729 });
730 playItem->setPosition(layerSize.width * 0.5f, layerSize.height * 0.5f);
731 addChild(playItem);
732
733 return true;
734 }
735
736 return false;
737}
738
739std::string AudioIssue11143Test::title() const
740{
741 return "Test for issue 11143";
742}
743
745{
746 return "2 seconds after first sound play,you should hear another sound.";
747}
748
749// Enable profiles for this file
750#undef CC_PROFILER_DISPLAY_TIMERS
751#define CC_PROFILER_DISPLAY_TIMERS() Profiler::getInstance()->displayTimers()
752#undef CC_PROFILER_PURGE_ALL
753#define CC_PROFILER_PURGE_ALL() Profiler::getInstance()->releaseAllTimers()
754
755#undef CC_PROFILER_START
756#define CC_PROFILER_START(__name__) ProfilingBeginTimingBlock(__name__)
757#undef CC_PROFILER_STOP
758#define CC_PROFILER_STOP(__name__) ProfilingEndTimingBlock(__name__)
759#undef CC_PROFILER_RESET
760#define CC_PROFILER_RESET(__name__) ProfilingResetTimingBlock(__name__)
761
762#undef CC_PROFILER_START_CATEGORY
763#define CC_PROFILER_START_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingBeginTimingBlock(__name__); } while(0)
764#undef CC_PROFILER_STOP_CATEGORY
765#define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingEndTimingBlock(__name__); } while(0)
766#undef CC_PROFILER_RESET_CATEGORY
767#define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingResetTimingBlock(__name__); } while(0)
768
769#undef CC_PROFILER_START_INSTANCE
770#define CC_PROFILER_START_INSTANCE(__id__, __name__) do{ ProfilingBeginTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
771#undef CC_PROFILER_STOP_INSTANCE
772#define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do{ ProfilingEndTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
773#undef CC_PROFILER_RESET_INSTANCE
774#define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do{ ProfilingResetTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
775
777{
778 if (AudioEngineTestDemo::init())
779 {
780 std::vector<std::string> audioFiles = {
781 "audio/SoundEffectsFX009/FX081.mp3",
782 "audio/SoundEffectsFX009/FX082.mp3",
783 "audio/SoundEffectsFX009/FX083.mp3",
784 "audio/SoundEffectsFX009/FX084.mp3",
785 "audio/SoundEffectsFX009/FX085.mp3",
786 "audio/SoundEffectsFX009/FX086.mp3",
787 "audio/SoundEffectsFX009/FX087.mp3",
788 "audio/SoundEffectsFX009/FX088.mp3",
789 "audio/SoundEffectsFX009/FX089.mp3",
790 "audio/SoundEffectsFX009/FX090.mp3"
791 };
792
793 for (const auto& audioFile : audioFiles)
794 {
795 AudioEngine::preload(audioFile);
796 }
797
798 auto& layerSize = this->getContentSize();
799
800 auto playItem = TextButton::create("Start Test", [this, audioFiles](TextButton* button){
801 button->setEnabled(false);
802 static_cast<TextButton*>(getChildByName("DisplayButton"))->setEnabled(true);
803
804 unschedule("test");
805 schedule([audioFiles](float dt){
806 int index = cocos2d::random(0, (int)(audioFiles.size()-1));
807 CC_PROFILER_START("play2d");
808 AudioEngine::play2d(audioFiles[index]);
809 CC_PROFILER_STOP("play2d");
810 }, 0.25f, "test");
811 });
812 playItem->setPosition(layerSize.width * 0.5f, layerSize.height * 2 / 3);
813 playItem->setName("PlayButton");
814 addChild(playItem);
815
816 auto displayItem = TextButton::create("Display Result", [this, playItem](TextButton* button){
817 unschedule("test");
818 AudioEngine::stopAll();
820 playItem->setEnabled(true);
821 button->setEnabled(false);
822 });
823 displayItem->setEnabled(false);
824 displayItem->setPosition(layerSize.width * 0.5f, layerSize.height / 3);
825 displayItem->setName("DisplayButton");
826 addChild(displayItem);
827
828 return true;
829 }
830
831 return false;
832}
833
835{
836 return "Test Performance of AudioEngine::play2d, audio is played 1 time per second";
837}
838
840{
841 return "Please see console for the result";
842}
843
845
847{
848 if (AudioEngineTestDemo::init())
849 {
850 schedule([](float dt){
851
852 AudioEngine::uncacheAll();
853 AudioEngine::preload("audio/SoundEffectsFX009/FX081.mp3");
854 AudioEngine::play2d("audio/SoundEffectsFX009/FX082.mp3");
855 AudioEngine::play2d("audio/LuckyDay.mp3");
856
857 }, 0.01f, "AudioSwitchStateTest");
858
859 return true;
860 }
861
862 return false;
863}
864
866{
867 return "play, preload, stop switch test";
868}
869
871{
872 return "Should not crash. No sound is ok";
873}
874
876
878{
879 if (AudioEngineTestDemo::init())
880 {
881 AudioEngine::play2d("audio/SmallFile.mp3");
882 return true;
883 }
884
885 return false;
886}
887
888std::string AudioSmallFileTest::title() const
889{
890 return "Playing small mp3 file";
891}
892
894{
895 return "Should not crash";
896}
897
900{
902
903 schedule([](float dt){
904 AudioEngine::play2d("audio/SmallFile2.mp3");
905 }, 0.08f, "smallfile2");
906}
907
908std::string AudioSmallFile2Test::title() const
909{
910 return "Play small mp3 file 2";
911}
912
914{
915 return "Should not crash and should not have rasp!";
916}
917
920{
922
923 schedule([](float dt){
924 AudioEngine::play2d("audio/SmallFile3.mp3");
925 }, 0.5f, "smallfile3");
926}
927
928std::string AudioSmallFile3Test::title() const
929{
930 return "Play small mp3 file 3";
931}
932
934{
935 return "Should not crash!";
936}
937
940{
942
943 int audioId = AudioEngine::play2d("audio/SoundEffectsFX009/FX082.mp3");
944 AudioEngine::pause(audioId);
945 AudioEngine::resume(audioId);
946
947 for (int i = 0; i < 10; ++i)
948 {
949 AudioEngine::pause(audioId);
950 AudioEngine::resume(audioId);
951 }
952}
953
955{
956 return "pause & resume right after play2d";
957}
958
960{
961 return "Should not crash";
962}
963
966{
968
969 AudioEngine::play2d("audio/EntireFramesTest.mp3");
970}
971
972std::string AudioIssue16938Test::title() const
973{
974 return "Issue 16938 Test";
975}
976
978{
979 return "Should heard the entire audio frames";
980}
981
984{
986
987 for (int i = 0; i < 10; ++i)
988 {
989 AudioEngine::preload("audio/SoundEffectsFX009/FX082.mp3", [i](bool isSucceed){
990 log("111: %d preload %s", i, isSucceed ? "succeed" : "failed");
991 AudioEngine::preload("audio/SoundEffectsFX009/FX082.mp3", [i](bool isSucceed){
992 log("222: %d preload %s", i, isSucceed ? "succeed" : "failed");
993 AudioEngine::preload("audio/SoundEffectsFX009/FX082.mp3", [i](bool isSucceed){
994 log("333: %d preload %s", i, isSucceed ? "succeed" : "failed");
995 });
996 });
997 });
998 }
999}
1000
1002{
1003 return "Preload same file multiple times";
1004}
1005
1007{
1008 return "Should not crash";
1009}
1010
1012{
1014
1015 auto fileUtils = FileUtils::getInstance();
1016 std::string writablePath = fileUtils->getWritablePath();
1017 std::string musicFile = "background.mp3";
1018 std::string saveFilePath = writablePath + "background_in_writable_dir.mp3";
1019
1020 _oldSearchPaths = fileUtils->getOriginalSearchPaths();
1021 fileUtils->addSearchPath(writablePath, true);
1022
1023 if (!fileUtils->isFileExist(saveFilePath))
1024 {
1025 Data data = fileUtils->getDataFromFile(musicFile);
1026 FILE* fp = fopen(saveFilePath.c_str(), "wb");
1027 if (fp != nullptr)
1028 {
1029 fwrite(data.getBytes(), data.getSize(), 1, fp);
1030 fclose(fp);
1031 }
1032 }
1033
1034 AudioEngine::play2d(saveFilePath);
1035}
1036
1038{
1040
1041 FileUtils::getInstance()->setSearchPaths(_oldSearchPaths);
1042}
1043
1045{
1046 return "Play audio in writable path";
1047}
1048
1050{
1051 return "Could play audio";
1052}
1053
1054//
1056{
1058
1059 auto item = MenuItemFont::create("Play 3 files one by one", [this](Ref* sender){
1060 playMusic("background.mp3");
1061 playMusic("background.mp3");
1062 playMusic("background.mp3");
1063 });
1064
1065 item->setPosition(VisibleRect::center());
1066
1067 auto menu = Menu::create(item, nullptr);
1068 menu->setPosition(Vec2::ANCHOR_BOTTOM_LEFT);
1069 addChild(menu);
1070}
1071
1073{
1075}
1076
1078{
1079 return "Click menu item to play 3 audio files";
1080}
1081
1083{
1084 return "After played over, click again, should also hear 3 audios";
1085}
1086
1087void AudioPlayInFinishedCB::doPlay(const std::string& filename)
1088{
1089 int playID = AudioEngine::play2d(filename, false, 1);
1090 AudioEngine::setFinishCallback(playID, [this](int finishID, const std::string& file){
1091 _playList.pop_front();
1092 log("finish music %s",file.c_str());
1093 if (!_playList.empty()) {
1094 const std::string& name = _playList.front();
1095 doPlay(name);
1096 }
1097 });
1098}
1099
1100void AudioPlayInFinishedCB::playMusic(const std::string& filename)
1101{
1102 _playList.push_back(filename);
1103 if (_playList.size() == 1) {
1104 doPlay(filename);
1105 }
1106}
1107
1108//
1110{
1112
1113 int id = AudioEngine::play2d("background.mp3");
1114 AudioEngine::setFinishCallback(id, [](int i, const std::string& str){
1115 AudioEngine::uncacheAll();
1116 });
1117}
1118
1120{
1122}
1123
1125{
1126 return "UncacheAll in finshed callback";
1127}
1128
1130{
1131 return "Should not crash";
1132}
1133
1134
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
#define CC_PROFILER_STOP(__name__)
#define CC_PROFILER_DISPLAY_TIMERS()
#define CC_PROFILER_START(__name__)
virtual void update(float dt) override
virtual bool init() override
virtual std::string title() const override
cocos2d::Label * _playOverLabel
virtual std::string title() const override
std::shared_ptr< bool > _isDestroyed
virtual void onBackCallback(cocos2d::Ref *sender) override
virtual void onExit() override
virtual bool init() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual bool init() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual bool init() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual bool init() override
virtual void onEnter() override
std::vector< std::string > _oldSearchPaths
virtual std::string subtitle() const override
virtual void onExit() override
virtual std::string title() const override
std::list< std::string > _playList
virtual void onExit() override
virtual void onEnter() override
void doPlay(const std::string &filename)
virtual std::string title() const override
virtual std::string subtitle() const override
void playMusic(const std::string &filename)
virtual std::string subtitle() const override
virtual std::string title() const override
std::string _files[FILE_COUNT]
cocos2d::AudioProfile _audioProfile
virtual std::string subtitle() const override
cocos2d::Label * _showLabel
virtual void update(float dt) override
virtual std::string title() const override
static const int FILE_COUNT
virtual bool init() override
virtual std::string title() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
virtual bool init() override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual bool init() override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onExit() override
virtual std::string subtitle() const override
virtual bool init() override
virtual std::string title() const override
virtual std::string title() const override
virtual bool init() override
std::string _files[TEST_COUNT]
static const int TEST_COUNT
virtual std::string title() const override
virtual bool init() override
virtual void onEnter() override
Definition: BaseTest.cpp:430
virtual void onBackCallback(cocos2d::Ref *sender)
Definition: BaseTest.cpp:481
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69