AudioEngine docs?

hI !

i want to do a volume fade for my bgm when start and end a scene… however, there is no implementation for volume change on SimpleAudioEngine, so i need to switch to AudioEngine, but i could not find documentation how to use it, i was seeing API the only function i check to play sound is play2d, i tried to play my music and indeed works (i haven’t played with volume things yet) the problem is that seems i can´t to play two diferents audio at same time because i try i get this:

I/AudioEngine-Win32 (171): OpenAL was initialized successfully!
V/AudioCache (71): AudioCache() 0F1F3D7C, id=1
V/AudioCache (129): readDataTask begin, cache id=1
V/AudioPlayer (147): AudioPlayer::play2d, _alSource: 1, player id=1
V/AudioCache (294): readDataTask end, cache id=1
V/AudioCache (71): AudioCache() 0F1F44CC, id=2
V/AudioCache (129): readDataTask begin, cache id=2
D/AudioEngine-Win32 (280): AudioEngineImpl::_play2d, cache was destroyed or not ready!
V/AudioCache (294): readDataTask end, cache id=2
V/AudioPlayer (69): ~AudioPlayer() (0F19BDF8), id=2
V/AudioPlayer (83): AudioPlayer::destroy begin, id=2
V/AudioPlayer (128): Before alSourceStop
V/AudioPlayer (130): Before alSourcei
V/AudioPlayer (136): AudioPlayer::destroy end, id=2

You can of course change the volume…please use the API docs: https://docs.cocos2d-x.org/api-ref/cplusplus/v3x/de/d8f/class_cocos_denshion_1_1_simple_audio_engine.html

We do need better AudioEngine docs, perhaps I can find time to write this up this week. Let me look at my schedule.

1 Like

Thanks my friend, the SimpleAudioEngine has the member functions setBackgroundMusicVolume, but it isi empty on definition…

AudioEngine only supported some audio formats like mp3, ogg and mid, wav does not works, but that is not a problem since i use ogg.

Thanks for your time to try writing these docs…

Cheers.

Just pass in a float. I’m not sure what you mean by empty.

setBackgroundMusicVolume (float volume)

1 Like

Take a look at SimpleAudioEngine.h line 264

Just post the code you question :slight_smile:

Well, regarding to my question, i solved it changing from wav to mp3 (wav was recognized by SimpleAudioEngine, and mp3 by AudioEngine)… so the main thread point is solved!..

So, as i´m using AudioEngine rather than SimpleAudioEngine, the volume is solved too…
Just was a bit curious about how it works thats why i was looking for docs…

This is what i have for my try to do bgm fade…

	
	if (source_vol == target_vol)	return;

	float volval = source_vol;
	cocos2d::experimental::AudioEngine::setVolume(0, volval);

	cocos2d::CallFunc* a0 = nullptr;

	if (source_vol < target_vol) {
		log(volval);
		a0 = cocos2d::CallFunc::create([&]() {
			
			volval += 0.1f;
			cocos2d::experimental::AudioEngine::setVolume(0, volval);
		});
	}
	else {
		log(volval);
		a0 = cocos2d::CallFunc::create([&]() {
			
			volval -= 0.1f;
			cocos2d::experimental::AudioEngine::setVolume(0, volval);
		});
	}

	cocos2d::DelayTime* de0 = cocos2d::DelayTime::create(0.3f);
	cocos2d::Sequence* seq = cocos2d::Sequence::create(a0, de0, nullptr);
	runAction(cocos2d::Repeat::create(seq, 10));
}```

SimpleAudio supports mp3 too

Yes.

Now i checked for another way to acchieve this fade, i was looking the MathUtil::lerp() function that could modify the float volume value, but does it needs to be executed inside some kind of update function?

How often do you want to fade the music? update() runs every frame.

that is good…
only need to fade at scene start and end…
i will give a try to use update() about a very little portion of time at begin and end of the scene… it is just to avoid abrupt music stop…

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.