gpt4 book ai didi

android - 设置Cocos2d-X Simple Audio Engine音量时遇到问题?

转载 作者:行者123 更新时间:2023-12-03 02:00:05 31 4
gpt4 key购买 nike

我正在Marmalade SDK 7.8.0之上使用Cocos2d-X 2.2.6开发游戏,但在设置Cocos Denshion简单音频引擎的声音效果音量时遇到一些问题。

我的代码是这样的:

void AudioHelper::init()
{
s3eResult result;

context = GameContext::getInstance();
s3eSoundSetInt(S3E_SOUND_DEFAULT_FREQ, 44100);

audioEngine = SimpleAudioEngine::sharedEngine();

preloadSoundEffects();

setVolume(context->getSoundVolume());
}

void AudioHelper::preloadSoundEffects()
{
unsigned int i;
CCArray *keysArray;
CCString *key;
const CCString *sound;

keysArray = soundEffects->allKeys();

for (i = 0; i < keysArray->count(); i++)
{
key = (CCString *) keysArray->objectAtIndex(i);
sound = soundEffects->valueForKey(key->getCString());

audioEngine->preloadEffect(sound->getCString());
}
}

void AudioHelper::setVolume(int volumeLevel)
{
float volume;

// Note: there are 6 volume levels in my game, they go from
// 0 (max volume) to 5 (no sound).
volume = 1 - volumeLevel / 5.0;

audioEngine->setEffectsVolume(volume);
}

void AudioHelper::playSoundEffect(const char *effectKey)
{
const CCString *sound;
int volume;

sound = soundEffects->valueForKey(effectKey);

if (sound != NULL)
audioEngine->playEffect(sound->getCString());

}

问题是,当我调用setVolume方法时,除非将音量级别参数设置为5(无声音),否则声音效果的音量不会改变。发生这种情况时,不会播放声音(按预期)。

我做错了什么?

最佳答案

您是否在此代码中记录了实际值?

void AudioHelper::setVolume(int volumeLevel)
{
float volume;

// Note: there are 6 volume levels in my game, they go from
// 0 (max volume) to 5 (no sound).
volume = 1 - volumeLevel / 5.0;

audioEngine->setEffectsVolume(volume);
}

您不能以这种方式混合使用float和int计算。此处的 float volume始终为1或0,因为您将 volumeLevel用作int类型。您的代码应如下所示:
void AudioHelper::setVolume(int volumeLevel)
{
float volume;

// Note: there are 6 volume levels in my game, they go from
// 0 (max volume) to 5 (no sound).
volume = 1.0f - (float)volumeLevel / 5.0f;

audioEngine->setEffectsVolume(volume);
}

还应考虑到在win平台上使用CocosDenshion有一些限制(那里的音量没有变化)。

关于android - 设置Cocos2d-X Simple Audio Engine音量时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378362/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com