The realization of hardware volume control by pressing keys in cocos2d-x

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.