main.xml布局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="播放音樂" />
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ToggleButton android:id="@+id/tbMute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOn="靜音"
android:textOff="正常"
android:checked="true"
android:layout_gravity="center_vertical" />
<Button android:id="@+id/btnUpper"
android:text="增大音量"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btnLower"
android:text="減小音量"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
AudioActivity類
package com.ljq.activity;
import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class AudioActivity extends Activity {
private Button btnPlay=null, btnUpper=null, btnLower=null;
private ToggleButton tbMute=null;
private MediaPlayer mediaPlayer=null; //聲頻
private AudioManager audioManager=null; //音頻
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
audioManager=(AudioManager)getSystemService(Service.AUDIO_SERVICE);
btnPlay=(Button)findViewById(R.id.btnPlay);
btnUpper=(Button)findViewById(R.id.btnUpper);
btnLower=(Button)findViewById(R.id.btnLower);
btnPlay.setOnClickListener(listener);
btnUpper.setOnClickListener(listener);
btnLower.setOnClickListener(listener);
tbMute=(ToggleButton)findViewById(R.id.tbMute);
tbMute.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, !isChecked); //設定是否靜音
}
});
}
View.OnClickListener listener=new View.OnClickListener(){
public void onClick(View v) {
@SuppressWarnings("unused")
Button btn=(Button)v;
switch (v.getId()) {
case R.id.btnPlay://播放音樂
mediaPlayer=MediaPlayer.create(AudioActivity.this, R.raw.music);
mediaPlayer.setLooping(true);//設定迴圈播放
mediaPlayer.start();//播放聲音
break;
case R.id.btnUpper://增多音量
//adjustStreamVolume: 調整指定聲音類型的音量
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI); //調高聲音
break;
case R.id.btnLower://減少音量
//第一個參數:聲音類型
//第二個參數:調整音量的方向
//第三個參數:可選的標誌位
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);//調低聲音
break;
}
}
};
}
運行結果