前幾篇介紹過SeekBar,本篇來結合聲音控制,透過拖拉桿連動系統音量控制。
透過AudioManager可以達成對聲音的控制。
AudioManager audioManager;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
方法:
//最大音量
int MaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//當前音量
int CurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
方法:
adjustStreamVolume(int streamType, int direction, int flags)
參數:
//增加音量
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
//降低音量
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
//增加音量
btn_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
initSeekBar();
}
});
//降低音量
btn_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
initSeekBar();
}
});
SeekBar.setOnSeekBarChangeListener提供三個狀態的回傳,分別為onProgressChanged、onStartTrackingTouch、onStopTrackingTouch。
onProgressChanged,只要SeekBar的值有變動時就會觸發,並且會回傳SeekBar當前的值,將這個值progress放入方法setStreamVolume(),即可與音量控制達成連動
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//當拖動SeekBar或造成SeekBar位置改變時執行
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//按下SeekBar時執行
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//放開SeekBar時執行
}
});
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="155dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="增加音量"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />
<Button
android:id="@+id/btn_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="降低音量"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_up" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.it_demo_audio;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
AudioManager audioManager;
SeekBar seekBar;
Button btn_up,btn_down;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
seekBar = findViewById(R.id.seekBar);
btn_up = findViewById(R.id.btn_up);
btn_down = findViewById(R.id.btn_down);
initSeekBar();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//當拖動SeekBar或造成SeekBar位置改變時執行
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//按下SeekBar時執行
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//放開SeekBar時執行
}
});
//增加音量
btn_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
initSeekBar();
}
});
//降低音量
btn_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC
,AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
initSeekBar();
}
});
}
//獲取當前音量與最大音量
private void initSeekBar() {
//最大音量
int MaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//目前音量
int CurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
seekBar.setMax(MaxVolume);
seekBar.setProgress(CurrentVolume);
}
}