AudioManager类:调节系统各种音量

效果图:

AudioManager类:调节系统各种音量_第1张图片

方法解析:

增大/降低音量值

public void adjustStreamVolume(int streamType, int direction, int flags)

设置音量值

public void setStreamVolume(int streamType, int index, int flags)

参数介绍

streamType是需要调整音量的类型,可以是:
STREAM_ALARM 警报
STREAM_MUSIC 音乐回放即媒体音量
STREAM_NOTIFICATION 窗口顶部状态栏Notification,
STREAM_RING 铃声
STREAM_SYSTEM 系统
STREAM_VOICE_CALL 通话
STREAM_DTMF 双音多频

direction是调整的方向,增加或减少,可以是:
ADJUST_LOWER 降低音量
ADJUST_RAISE 升高音量
ADJUST_SAME 保持不变,这个主要用于向用户展示当前的音量

flags是一些附加参数,只介绍两个常用的
FLAG_PLAY_SOUND 调整音量时播放声音
FLAG_SHOW_UI 调整时显示音量条

index是要设置的音量值,类型为int

示例代码

public class SunActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private int VOICE_TYPE;
    private SeekBar sb_media, sb_calling, sb_system, sb_clock;
    private AudioManager audioManager;
    private int startPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sun);

        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        sb_media = (SeekBar) findViewById(R.id.sb_media);
        sb_calling = (SeekBar) findViewById(R.id.sb_ring);
        sb_system = (SeekBar) findViewById(R.id.sb_system);
        sb_clock = (SeekBar) findViewById(R.id.sb_alarm);

        //为了SeekBar的效果明显,将音量值乘以10显示
        sb_media.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) * 10);
        sb_calling.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_RING) * 10);
        sb_system.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM) * 10);
        sb_clock.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM) * 10);

        sb_media.setOnSeekBarChangeListener(this);
        sb_calling.setOnSeekBarChangeListener(this);
        sb_system.setOnSeekBarChangeListener(this);
        sb_clock.setOnSeekBarChangeListener(this);
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        startPoint = seekBar.getProgress();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        switch (seekBar.getId()) {
            case R.id.sb_media:
                VOICE_TYPE = AudioManager.STREAM_MUSIC;
                break;
            case R.id.sb_ring:
                VOICE_TYPE = AudioManager.STREAM_RING;
                break;
            case R.id.sb_system:
                VOICE_TYPE = AudioManager.STREAM_SYSTEM;
                break;
            case R.id.sb_alarm:
                VOICE_TYPE = AudioManager.STREAM_ALARM;
                break;
        }

//        if (seekBar.getProgress() > startPoint) { //提升音量
//            audioManager.adjustStreamVolume(VOICE_TYPE, AudioManager.ADJUST_RAISE, 0);
//        } else {//降低音量
//            audioManager.adjustStreamVolume(VOICE_TYPE, AudioManager.ADJUST_LOWER, 0);
//        }

        audioManager.setStreamVolume(VOICE_TYPE, seekBar.getProgress() / 10, AudioManager.FLAG_SHOW_UI);
    }
}

部分布局代码

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:background="@drawable/shape_text"
            android:padding="3dp"
            android:text="媒体"
            android:textColor="#ffffff" />

        <SeekBar
            android:id="@+id/sb_media"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    LinearLayout>

你可能感兴趣的:(AudioManager类:调节系统各种音量)