Article connection (http://blog.csdn.net/keven418440201/article/details/9138721)
Recently in the use of cocos2d-x to write a small game, quickly found that the game background music and sound volume is not subject to the phone (Android OS) volume adjustment keys control. After several twists and turns to find that the cocos2d-x did not achieve the relevant functions (personal opinions, there are other parties to achieve can contact me, Thank you), so finally modified the cocos2d-x In the android terminal source code (cocos2dxactivity ), the implementation method is as follows:
private AudioManager mAudioManager;......@Overrideprotected void onCreate(final Bundle savedInstanceState) {super.onCreate(savedInstanceState);...... mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);......}......@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);switch(keyCode){case KeyEvent.KEYCODE_VOLUME_UP:mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume+1, 1);return true;case KeyEvent.KEYCODE_VOLUME_DOWN:mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume-1, 1);return true;}return super.onKeyDown(keyCode, event);}
Finally, do not forget to add permissions:
<Uses-Permission Android: Name = "android. Permission. modify_audio_settings"/>
Based on the above method, you can test the mobile phone volume adjustment button in Android OS to control the game volume normally.
I personally found a stupid way. If you have other good methods, please leave a message.