Android to adjust the sound size

Source: Internet
Author: User

Android Adjusts the sound size 1. Some knowledge of background music

There are a lot of classes on the internet for adding background music:
Mediaplayer,soundpool,audiomanager, but sometimes we can't solve the problems we're having in development.
I'll focus on the experience of adjusting the volume.

1:android Adjust the volume there are many can adjust the volume of the system, the volume of the media, the volume of the alarm, then you need to use the Audiomanager class
首先是得到AudioManager实例:
    • 1
    • 2
    AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    • 1
2. Adjust the volume method there are two, one is progressive, that is, like manually press volume keys, step by step increase or decrease, the other is to set the volume value directly.1. The first step is the method:
**public void adjustStreamVolume (int streamType, int direction, int flags); **
    • 1
    • 2
    am.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
    • 1
  • Explain three parameters

    The first streamtype is the type of volume you want to adjust, which is set to the media volume, which can be:

    Field Description
    Stream_alarm Alerts
    Stream_music Music playback is media volume
    Stream_notification The status bar at the top of the window notification,
    Stream_ring Ring
    Stream_system System
    Stream_voice_call Call
    Stream_dtmf Dual tone multi-frequency, not very clear what things

    The second direction, which is to adjust the direction, increase or decrease, can be:

    Field Description
    Adjust_lower Lower Volume
    Adjust_raise Raise the volume
    Adjust_same This is used primarily to show the user the current volume level

    The third flags are additional parameters that describe only two common

    Field Description
    Flag_play_sound Play a sound when you adjust the volume
    flag_show_ui Display the volume bar when adjusting, the one that appears when you press the volume key
2. Directly set the volume is worth the method:
public void setStreamVolume (int streamType, int index, int flags) { am.setStreamVolume(AudioManager.STREAM_MUSIC, /*修改第二个参数为一个固定的值,就是设置成功。 下面的方法:获取音乐类型的音频流的最大值*/ am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND); }

Android to adjust the sound size

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.