Android實(shí)現(xiàn)音量調(diào)節(jié)的方法
本文實(shí)例講述了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法。分享給大家供大家參考。具體如下:
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); //設(shè)置是否靜音
}
});
}
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);//設(shè)置循環(huán)播放
mediaPlayer.start();//播放聲音
break;
case R.id.btnUpper://增多音量
//adjustStreamVolume: 調(diào)整指定聲音類型的音量
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI); //調(diào)高聲音
break;
case R.id.btnLower://減少音量
//第一個參數(shù):聲音類型
//第二個參數(shù):調(diào)整音量的方向
//第三個參數(shù):可選的標(biāo)志位
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);//調(diào)低聲音
break;
}
}
};
}
運(yùn)行結(jié)果:

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽事件的方法
- android編程獲取和設(shè)置系統(tǒng)鈴聲和音量大小的方法
- Android 自定義SeekBar動態(tài)改變硬件音量大小實(shí)現(xiàn)和音量鍵的同步(推薦)
- Android seekbar(自定義)控制音量同步更新
- Android使用AudioManager修改系統(tǒng)音量的方法
- Android 使用Vitamio打造自己的萬能播放器(2)—— 手勢控制亮度、音量、縮放
- Android中自定義View實(shí)現(xiàn)圓環(huán)等待及相關(guān)的音量調(diào)節(jié)效果
- Android系統(tǒng)音量條實(shí)例代碼
相關(guān)文章
Android實(shí)現(xiàn)讀取NFC卡卡號示例
本篇文章主要介紹了Android實(shí)現(xiàn)讀取NFC卡卡號示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng)
這篇文章主要為大家詳細(xì)介紹了ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android檢測手機(jī)中存儲卡及剩余空間大小的方法(基于Environment,StatFs及DecimalFormat
這篇文章主要介紹了Android檢測手機(jī)中存儲卡及剩余空間大小的方法,基于Environment,StatFs及DecimalFormat實(shí)現(xiàn)該功能,具有一定參考借鑒價值,需要的朋友可以參考下2016-01-01
Android的RV列表刷新詳解Payload與Diff方式異同
這篇文章主要為大家介紹了Android的RV列表刷新詳解Payload與Diff方式異同,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼
這篇文章主要介紹了Android 使用 DowanloadManager 實(shí)現(xiàn)下載并獲取下載進(jìn)度實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06

