AudioControlTest
| 227 | |
| 228 | // AudioControlTest |
| 229 | bool AudioControlTest::init() |
| 230 | { |
| 231 | auto ret = AudioEngineTestDemo::init(); |
| 232 | _audioID = AudioEngine::INVALID_AUDIO_ID; |
| 233 | _loopEnabled = false; |
| 234 | _volume = 1.0f; |
| 235 | _duration = AudioEngine::TIME_UNKNOWN; |
| 236 | _timeRatio = 0.0f; |
| 237 | _updateTimeSlider = true; |
| 238 | _isStopped = false; |
| 239 | |
| 240 | std::string fontFilePath = "fonts/arial.ttf"; |
| 241 | |
| 242 | auto& layerSize = this->getContentSize(); |
| 243 | |
| 244 | _playOverLabel = Label::createWithSystemFont("Play Over", "", 30); |
| 245 | _playOverLabel->setPosition(Vec2(layerSize / 2) + Vec2(0, 30)); |
| 246 | _playOverLabel->setVisible(false); |
| 247 | addChild(_playOverLabel, 99999); |
| 248 | |
| 249 | auto playItem = TextButton::create("play", [&](TextButton* button) { |
| 250 | if (_audioID == AudioEngine::INVALID_AUDIO_ID) |
| 251 | { |
| 252 | _audioID = AudioEngine::play2d("background.mp3", _loopEnabled, _volume); |
| 253 | |
| 254 | if (_audioID != AudioEngine::INVALID_AUDIO_ID) |
| 255 | { |
| 256 | _isStopped = false; |
| 257 | |
| 258 | button->setEnabled(false); |
| 259 | AudioEngine::setFinishCallback(_audioID, [&](int id, std::string_view filePath) { |
| 260 | AXLOGD("_audioID({}), _isStopped:({}), played over!!!", _audioID, _isStopped); |
| 261 | |
| 262 | _playOverLabel->setVisible(true); |
| 263 | |
| 264 | scheduleOnce([&](float dt) { _playOverLabel->setVisible(false); }, 2.0f, "hide_play_over_label"); |
| 265 | |
| 266 | assert(!_isStopped); // Stop audio should not trigger finished callback |
| 267 | _audioID = AudioEngine::INVALID_AUDIO_ID; |
| 268 | ((TextButton*)_playItem)->setEnabled(true); |
| 269 | |
| 270 | _timeRatio = 0.0f; |
| 271 | ((SliderEx*)_timeSlider)->setRatio(_timeRatio); |
| 272 | }); |
| 273 | } |
| 274 | } |
| 275 | }); |
| 276 | _playItem = playItem; |
| 277 | playItem->setPosition(layerSize.width * 0.3f, layerSize.height * 0.8f); |
| 278 | addChild(playItem); |
| 279 | |
| 280 | auto stopItem = TextButton::create("stop", [&](TextButton* button) { |
| 281 | if (_audioID != AudioEngine::INVALID_AUDIO_ID) |
| 282 | { |
| 283 | _isStopped = true; |
| 284 | AudioEngine::stop(_audioID); |
| 285 | |
| 286 | _audioID = AudioEngine::INVALID_AUDIO_ID; |
no test coverage detected