初学cocos2d-x,按照教程写音量调整按钮,发现最后无论如何按钮都无法生效。找来找去发现是setBackGroundMusicVolume函数下竟然没有实现。结合一些文章,貌似是在win7下可能会出类似问题,而Mac OS似乎没啥问题……好歧视啊。后来在论坛上找到了响应的解决方案(原帖地址http://www.cocos2d-x.org/forums/6/topics/11651),之后在里面的一个链接中,找到解决办法如下:
更改cocos2d-x目录下CocosDenshion/win32/Mciplayer.cpp,更改如下内容:
1 | 1 | #include "MciPlayer.h" |
2 | +#include <Digitalv.h> | |
2 | 3 | |
3 | 4 | #define WIN_CLASS_NAME "CocosDenshionCallbackWnd" |
4 | 5 | #define BREAK_IF(cond) if (cond) break; |
@@ -94,6 +95,26 @@ void MciPlayer::Open(const char* pFileName, UINT uId) | ||
94 | 95 | } while (0); |
95 | 96 | } |
96 | 97 | |
98 | +void MciPlayer::Volume(UINT volume) | |
99 | +{ | |
100 | + if (!m_hDev) | |
101 | + return; | |
102 | + MCI_DGV_SETAUDIO_PARMS mciParams = {0}; | |
103 | + mciParams.dwItem = MCI_DGV_SETAUDIO_VOLUME; | |
104 | + mciParams.dwValue = volume; | |
105 | + mciSendCommand(m_hDev, MCI_SETAUDIO, MCI_DGV_SETAUDIO_ITEM | MCI_DGV_SETAUDIO_VALUE, (DWORD)&mciParams); | |
106 | +} | |
107 | + | |
108 | +UINT MciPlayer::Volume() const | |
109 | +{ | |
110 | + if (!m_hDev) | |
111 | + return 0; | |
112 | + MCI_STATUS_PARMS mciParams = {0}; | |
113 | + mciParams.dwItem = MCI_DGV_STATUS_VOLUME; | |
114 | + mciSendCommand(m_hDev, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)&mciParams); | |
115 | + return mciParams.dwReturn; | |
116 | +} | |
117 | + | |
97 | 118 | void MciPlayer::Play(UINT uTimes /* = 1 */) |
98 | 119 | { |
99 | 120 | if (! m_hDev) |
然后修改CocosDenshion/win32/Mciplayer.h:
54 | 54 | */ |
55 | 55 | UINT GetSoundID(); |
56 | 56 | |
57 | + // @volume value ranges from 0 .. 1000 | |
58 | + void Volume(UINT volume); | |
59 | + | |
60 | + // @return value ranges from 0 .. 1000 | |
61 | + UINT MciPlayer::Volume() const; | |
62 | + | |
57 | 63 | private: |
58 | 64 | friend LRESULT WINAPI _SoundPlayProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); |
59 | 65 | |
最后修改CocosDenshion/win32/SimpleAudioEngine.cpp:
74 | 74 | { |
75 | 75 | return; |
76 | 76 | } |
77 | -
| |
77 | + | |
78 | 78 | sharedMusic().Open(_FullPath(pszFilePath).c_str(), _Hash(pszFilePath)); |
79 | 79 | sharedMusic().Play((bLoop) ? -1 : 1); |
80 | 80 | } |
@@ -235,20 +235,26 @@ void SimpleAudioEngine::unloadEffect(const char* pszFilePath) | ||
235 | 235 | |
236 | 236 | float SimpleAudioEngine::getBackgroundMusicVolume() |
237 | 237 | { |
238 | - return 1.0; | |
238 | + return sharedMusic().Volume() / 1000.0f; | |
239 | 239 | } |
240 | 240 | |
241 | 241 | void SimpleAudioEngine::setBackgroundMusicVolume(float volume) |
242 | 242 | { |
243 | + sharedMusic().Volume((UINT) (volume * 1000.0)); | |
243 | 244 | } |
244 | 245 | |
245 | 246 | float SimpleAudioEngine::getEffectsVolume() |
246 | 247 | { |
247 | - return 1.0; | |
248 | + return sharedList().at(0)->Volume() / 1000.0f; // XXX returning the first | |
248 | 249 | } |
249 | 250 | |
250 | 251 | void SimpleAudioEngine::setEffectsVolume(float volume) |
251 | 252 | { |
253 | + EffectList::iterator iter; | |
254 | + for (iter = sharedList().begin(); iter != sharedList().end(); iter++) | |
255 | + { | |
256 | + iter->second->Volume((UINT) (volume * 1000.0)); | |
257 | + } | |
252 | 258 | } |
253 | 259 |
之后函数就可以调整音量了。