PuzzleSDK
AudioControlTest类 参考

#include <NewAudioEngineTest.h>

+ 类 AudioControlTest 继承关系图:
+ AudioControlTest 的协作图:

Public 成员函数

 CREATE_FUNC (AudioControlTest)
 
virtual ~AudioControlTest ()
 
virtual bool init () override
 
virtual void update (float dt) override
 
virtual std::string title () const override
 
- Public 成员函数 继承自 AudioEngineTestDemo
 AudioEngineTestDemo ()
 
virtual void onExit () override
 
virtual void onBackCallback (cocos2d::Ref *sender) override
 
- Public 成员函数 继承自 TestCase
 TestCase ()
 
 ~TestCase ()
 
virtual std::string subtitle () const
 
virtual Type getTestType () const
 
virtual float getDuration () const
 
virtual std::string getExpectedOutput () const
 
virtual std::string getActualOutput () const
 
virtual void restartTestCallback (cocos2d::Ref *sender)
 
virtual void nextTestCallback (cocos2d::Ref *sender)
 
virtual void priorTestCallback (cocos2d::Ref *sender)
 
void setTestSuite (TestSuite *testSuite)
 
TestSuitegetTestSuite () const
 
float getRunTime () const
 
void setTestCaseName (const std::string &name)
 
std::string getTestCaseName () const
 
const cocos2d::Label * getSubtitleLable () const
 
const cocos2d::MenuItemImage * getRestartTestItem () const
 
virtual void onEnter () override
 

Private 属性

int _audioID
 
bool _loopEnabled
 
float _volume
 
float _duration
 
float _timeRatio
 
void * _playItem
 
void * _timeSlider
 
bool _updateTimeSlider
 
bool _isStopped
 
cocos2d::Label * _playOverLabel
 

额外继承的成员函数

- Public 类型 继承自 TestCase
enum class  Type { ROBUSTNESS , UNIT , GRAPHICAL_STATIC , MANUAL }
 
- Public 属性 继承自 TestCase
CC_CONSTRUCTOR_ACCESS __pad0__: virtual bool init() override
 
- Protected 属性 继承自 AudioEngineTestDemo
std::shared_ptr< bool > _isDestroyed
 
- Protected 属性 继承自 TestCase
cocos2d::MenuItemImage * _priorTestItem
 
cocos2d::MenuItemImage * _restartTestItem
 
cocos2d::MenuItemImage * _nextTestItem
 
cocos2d::Label * _titleLabel
 
cocos2d::Label * _subtitleLabel
 

详细描述

在文件 NewAudioEngineTest.h52 行定义.

构造及析构函数说明

◆ ~AudioControlTest()

AudioControlTest::~AudioControlTest ( )
virtual

在文件 NewAudioEngineTest.cpp385 行定义.

386{
387}

成员函数说明

◆ CREATE_FUNC()

AudioControlTest::CREATE_FUNC ( AudioControlTest  )

◆ init()

bool AudioControlTest::init ( )
overridevirtual

在文件 NewAudioEngineTest.cpp219 行定义.

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}
virtual void update(float dt) override
cocos2d::Label * _playOverLabel

引用了 _audioID, _duration, _isStopped, _loopEnabled, _playItem, _playOverLabel, _timeRatio, _timeSlider, _updateTimeSlider, _volume , 以及 update().

+ 函数调用图:

◆ title()

std::string AudioControlTest::title ( ) const
overridevirtual

重载 AudioEngineTestDemo .

在文件 NewAudioEngineTest.cpp389 行定义.

390{
391 return "Audio control test";
392}

◆ update()

void AudioControlTest::update ( float  dt)
overridevirtual

在文件 NewAudioEngineTest.cpp369 行定义.

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}

引用了 _audioID, _duration, _timeRatio, _timeSlider , 以及 _updateTimeSlider.

被这些函数引用 init().

+ 这是这个函数的调用关系图:

类成员变量说明

◆ _audioID

int AudioControlTest::_audioID
private

在文件 NewAudioEngineTest.h66 行定义.

被这些函数引用 init() , 以及 update().

◆ _duration

float AudioControlTest::_duration
private

在文件 NewAudioEngineTest.h69 行定义.

被这些函数引用 init() , 以及 update().

◆ _isStopped

bool AudioControlTest::_isStopped
private

在文件 NewAudioEngineTest.h75 行定义.

被这些函数引用 init().

◆ _loopEnabled

bool AudioControlTest::_loopEnabled
private

在文件 NewAudioEngineTest.h67 行定义.

被这些函数引用 init().

◆ _playItem

void* AudioControlTest::_playItem
private

在文件 NewAudioEngineTest.h72 行定义.

被这些函数引用 init().

◆ _playOverLabel

cocos2d::Label* AudioControlTest::_playOverLabel
private

在文件 NewAudioEngineTest.h76 行定义.

被这些函数引用 init().

◆ _timeRatio

float AudioControlTest::_timeRatio
private

在文件 NewAudioEngineTest.h70 行定义.

被这些函数引用 init() , 以及 update().

◆ _timeSlider

void* AudioControlTest::_timeSlider
private

在文件 NewAudioEngineTest.h73 行定义.

被这些函数引用 init() , 以及 update().

◆ _updateTimeSlider

bool AudioControlTest::_updateTimeSlider
private

在文件 NewAudioEngineTest.h74 行定义.

被这些函数引用 init() , 以及 update().

◆ _volume

float AudioControlTest::_volume
private

在文件 NewAudioEngineTest.h68 行定义.

被这些函数引用 init().


该类的文档由以下文件生成: