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è)計有所幫助。
- 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開發(fā)之Gradle?進(jìn)階Tasks深入了解
這篇文章主要為大家介紹了Android開發(fā)之Gradle?進(jìn)階Tasks深入了解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂胓enymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android中檢測當(dāng)前是否為主線程最可靠的解決方法
這篇文章主要介紹了Android中檢測當(dāng)前是否為主線程最可靠的解決方法,本文先是給出了最可靠的方法,然后給出了幾個實(shí)驗(yàn)例子,需要的朋友可以參考下2015-01-01adb wireless進(jìn)行Android手機(jī)調(diào)試詳解
這篇文章給大家講解了在Android手機(jī)上使用adb wireless進(jìn)行調(diào)試的步驟以及問題解決辦法,有需要的跟著學(xué)習(xí)下吧。2017-12-12Android ShareSDK快速實(shí)現(xiàn)分享功能
這篇文章主要介紹了Android ShareSDK快速實(shí)現(xiàn)分享功能的相關(guān)資料,需要的朋友可以參考下2016-02-02Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫訪問實(shí)例
下面小編就為大家分享一篇Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫訪問實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Android編程動態(tài)按鈕實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程動態(tài)按鈕實(shí)現(xiàn)方法,分享了onTouch方法及xml調(diào)用兩種實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10