[Turn]android system to obtain and set various volume

Source: Internet
Author: User

Get System Volume

Get the ringtone and volume of your Android phone through the program. Also, the way to set the ringtone and volume is simple!

The way to set the volume is also simple, Audiomanager provides the method:
Publicvoidsetstreamvolume (intstreamtype,intindex,intflags) where Streamtype has built-in constants, go to the documentation to see them.

Java code:
Audiomanager Maudiomanager = (audiomanager) getsystemservice (Context.audio_service);

Call volume

int max = Maudiomanager. Getstreammaxvolume (Audiomanager.stream_voice_call);
int current = Maudiomanager. Getstreamvolume (Audiomanager.stream_voice_call);
LOG.D ("Vioce_call", "Max:" + Max + "Current:" + current);

System Volume

max = Maudiomanager. Getstreammaxvolume (Audiomanager.stream_system);
Current = Maudiomanager. Getstreamvolume (Audiomanager.stream_system);
LOG.D ("SYSTEM", "Max:" + Max + "Current:" + current);

Ringtone Volume

max = Maudiomanager.getstreammaxvolume (audiomanager.stream_ring);
Current = Maudiomanager.getstreamvolume (audiomanager.stream_ring);
LOG.D ("RING", "Max:" + Max + "Current:" + current);

Music volume

max = Maudiomanager.getstreammaxvolume (audiomanager.stream_music);
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_music);
LOG.D ("MUSIC", "Max:" + Max + "Current:" + current);

Prompt for sound volume

max = Maudiomanager.getstreammaxvolume (audiomanager.stream_alarm);
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_alarm);
LOG.D ("ALARM", "Max:" + Max + "Current:" + current);
Ps:
Only the multimedia volume is allowed to be adjusted during the game, and the call volume is not allowed.
Setvolumecontrolstream (Audiomanager.stream_music);
Do not move for a long time, do not allow black screen, View.setkeepscreenon (true);
It is estimated that registration permission is required in the manifest file.

Adjust Media volume

AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);

OnKeyDown method for overriding Activity

?@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    switch (keyCode) {    case KeyEvent.KEYCODE_VOLUME_UP:        audio.adjustStreamVolume(            AudioManager.STREAM_MUSIC,            AudioManager.ADJUST_RAISE,            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);        return true;    case KeyEvent.KEYCODE_VOLUME_DOWN:        audio.adjustStreamVolume(            AudioManager.STREAM_MUSIC,            AudioManager.ADJUST_LOWER,            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);        return true;    default:        break;    }    return super.onKeyDown(keyCode, event);}

[Turn]android system to obtain and set various volume

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.