一個好的 Android 應用免不了會自帶背景音樂,比如遊戲或者一款比較不錯的書本閱讀器。一些好的應用在自帶音樂的時候會多新增一款小功能即可以幫助使用者設定聲音大小或者改變應用的聲音模式。
本篇基於 Android API 中的 AudioManager 作講述,使看過本篇的讀者可以迅速的掌握這個類的實現過程。下面是本篇大綱:
- 1、認識 AudioManager
- 2、AudioManager 主要方法介紹
- 3、程式邏輯實現過程
1、認識 AudioManager
AudioManager 類位於 android.Media 包中,該類提供訪問控制音量和鈐聲模式的操作。
2、AudioManager 主要方法介紹
郵於 AudioManager 該類方法過多,這裡只講述幾個比較常用到的方法:
- 方法:adjustVolume(int direction, int flags)
解釋:這個方法用來控制手機音量大小,當傳入的第一個引數為 AudioManager.ADJUST_LOWER 時,可將音量調小一個單位,傳入 AudioManager.ADJUST_RAISE 時,則可以將音量調大一個單位。 - 方法:getMode()
解釋:返回當前音訊模式。 - 方法:getRingerMode()
解釋:返回當前的鈴聲模式。 - 方法:getStreamVolume(int streamType)
解釋:取得當前手機的音量,最大值為7,最小值為0,當為0時,手機自動將模式調整為“震動模式”。 - 方法:setRingerMode(int ringerMode)
解釋:改變鈴聲模式
3、程式邏輯實現過程
介面上設定了一個圖片,表示當前鈴聲狀態,一個進度條表示當前音量大小,五個圖片按鈕,用來表示增加/減小音量、普通模式、靜音模式和震動模式。下面是介面的 XML 佈局程式碼:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="42px"
>
</TextView>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"
>
</ImageView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="102px"
>
</TextView>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"
>
</ProgressBar>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"
>
</ImageButton>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"
>
</ImageButton>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/normal"
>
</ImageButton>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"
>
</ImageButton>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"
>
</ImageButton>
</AbsoluteLayout>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="42px"
>
</TextView>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"
>
</ImageView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="102px"
>
</TextView>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"
>
</ProgressBar>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"
>
</ImageButton>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"
>
</ImageButton>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/normal"
>
</ImageButton>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"
>
</ImageButton>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"
>
</ImageButton>
</AbsoluteLayout>
程式類分別為:
1、viewHolder
介面上的所有控制元件和元素都在這裡靜態宣告
package com.terry;
import android.media.AudioManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class viewHolder {
public static ImageButton downButton;
public static ImageButton upButton;
public static ImageButton normalButton;
public static ImageButton muteButton;
public static ImageButton vibrateButton;
public static ProgressBar myProgressBar;
public static ImageView myImageView;
public static AudioManager audiomanage;
}
import android.media.AudioManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class viewHolder {
public static ImageButton downButton;
public static ImageButton upButton;
public static ImageButton normalButton;
public static ImageButton muteButton;
public static ImageButton vibrateButton;
public static ProgressBar myProgressBar;
public static ImageView myImageView;
public static AudioManager audiomanage;
}
2、AudioManagerActivity
程式入口處,和主要邏輯程式碼的處理,程式開頭以 (AudioManager)getSystemService(AUDIO_SERVICE); 取得 AudioManager 物件。然後再利用該物件來對鈴聲進行調整。宣告瞭一個返回 ImageButton 的方法,用來處理各自按鈕點選所執行的不同事件和對圖片的狀態進行調整設定。以下把程式碼提供給大家:
package com.terry;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class AudioManagerActivity extends Activity {
//音量變數
private int volume=0;
//聲音模式
private int mode;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
//通過getStreamVolume 獲得當前音量大小
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
//把當前音量的值 設定給進度條
viewHolder.myProgressBar.setProgress(volume);
//得到當前的聲音模式
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
viewHolder.downButton=btnListener(viewHolder.downButton);
viewHolder.upButton=btnListener(viewHolder.upButton);
viewHolder.muteButton=btnListener(viewHolder.muteButton);
viewHolder.normalButton=btnListener(viewHolder.normalButton);
viewHolder.vibrateButton=btnListener(viewHolder.vibrateButton);
}
//找到控制元件
void findview(){
viewHolder.downButton=(ImageButton)findViewById(R.id.downButton);
viewHolder.upButton=(ImageButton)findViewById(R.id.upButton);
viewHolder.muteButton=(ImageButton)findViewById(R.id.muteButton);
viewHolder.normalButton=(ImageButton)findViewById(R.id.normalButton);
viewHolder.vibrateButton=(ImageButton)findViewById(R.id.vibrateButton);
viewHolder.myImageView=(ImageView)findViewById(R.id.myImage);
viewHolder.myProgressBar=(ProgressBar)findViewById(R.id.myProgress);
viewHolder.audiomanage=(AudioManager)getSystemService(AUDIO_SERVICE);
}
//按鈕 的單擊事件
ImageButton btnListener(ImageButton b){
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.downButton:
viewHolder.audiomanage.adjustVolume(AudioManager.ADJUST_LOWER, 0);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
break;
case R.id.upButton:
viewHolder.audiomanage.adjustVolume(AudioManager.ADJUST_RAISE, 0);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
break;
case R.id.muteButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.mute));
break;
case R.id.normalButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.normal));
break;
case R.id.vibrateButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
break;
}
}
});
return b;
}
//設定圖片狀態
void setImageState(){
if(mode==AudioManager.RINGER_MODE_NORMAL)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
}
}
}
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class AudioManagerActivity extends Activity {
//音量變數
private int volume=0;
//聲音模式
private int mode;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
//通過getStreamVolume 獲得當前音量大小
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
//把當前音量的值 設定給進度條
viewHolder.myProgressBar.setProgress(volume);
//得到當前的聲音模式
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
viewHolder.downButton=btnListener(viewHolder.downButton);
viewHolder.upButton=btnListener(viewHolder.upButton);
viewHolder.muteButton=btnListener(viewHolder.muteButton);
viewHolder.normalButton=btnListener(viewHolder.normalButton);
viewHolder.vibrateButton=btnListener(viewHolder.vibrateButton);
}
//找到控制元件
void findview(){
viewHolder.downButton=(ImageButton)findViewById(R.id.downButton);
viewHolder.upButton=(ImageButton)findViewById(R.id.upButton);
viewHolder.muteButton=(ImageButton)findViewById(R.id.muteButton);
viewHolder.normalButton=(ImageButton)findViewById(R.id.normalButton);
viewHolder.vibrateButton=(ImageButton)findViewById(R.id.vibrateButton);
viewHolder.myImageView=(ImageView)findViewById(R.id.myImage);
viewHolder.myProgressBar=(ProgressBar)findViewById(R.id.myProgress);
viewHolder.audiomanage=(AudioManager)getSystemService(AUDIO_SERVICE);
}
//按鈕 的單擊事件
ImageButton btnListener(ImageButton b){
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.downButton:
viewHolder.audiomanage.adjustVolume(AudioManager.ADJUST_LOWER, 0);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
break;
case R.id.upButton:
viewHolder.audiomanage.adjustVolume(AudioManager.ADJUST_RAISE, 0);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
mode=viewHolder.audiomanage.getRingerMode();
setImageState();
break;
case R.id.muteButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.mute));
break;
case R.id.normalButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.normal));
break;
case R.id.vibrateButton:
viewHolder.audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
volume=viewHolder.audiomanage.getStreamVolume(AudioManager.STREAM_RING);
viewHolder.myProgressBar.setProgress(volume);
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
break;
}
}
});
return b;
}
//設定圖片狀態
void setImageState(){
if(mode==AudioManager.RINGER_MODE_NORMAL)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE)
{
viewHolder.myImageView.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
}
}
}
此類大致操作如上,歡迎提建議。
由於公司有加密軟體所以上傳不了附件。