2011年4月3日星期日

利用 AudioManager.getStreamVolume() 調整鈴聲音量

修改前面文章"更改鈴聲模式, AudioManager.setRingerMode()"的代碼, 添加顯示和調整鈴聲音量的功能.

調整鈴聲音量

package com.AndroidRinger;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidRinger extends Activity {

private AudioManager audioManager;
TextView textRingerState, textRingerVol;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonRinger = (Button)findViewById(R.id.buttonringer);
Button buttonRingerUp = (Button)findViewById(R.id.buttonringerup);
Button buttonRingerDown = (Button)findViewById(R.id.buttonringerdown);
textRingerState = (TextView)findViewById(R.id.textringerstate);
textRingerVol = (TextView)findViewById(R.id.textringervolume);

audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

buttonRinger.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int ringerMode = audioManager.getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_SILENT){
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}else{
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

displayRingMode();
}});

buttonRingerUp.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
audioManager.adjustStreamVolume(AudioManager.STREAM_RING,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_ALLOW_RINGER_MODES|AudioManager.FLAG_PLAY_SOUND);
displayRingMode();
}});

buttonRingerDown.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
audioManager.adjustStreamVolume(AudioManager.STREAM_RING,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_ALLOW_RINGER_MODES|AudioManager.FLAG_PLAY_SOUND);
displayRingMode();
displayRingMode();
}});
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
displayRingMode();
}

private void displayRingMode(){
int ringerMode = audioManager.getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_SILENT){
textRingerState.setText("Ringer is Silent");
}else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE){
textRingerState.setText("Ringer is Vibrate");
}else{
textRingerState.setText("Ringer is Normal");
}

int ringerVol = audioManager.getStreamVolume(AudioManager.STREAM_RING);
textRingerVol.setText("Ringer Volume: " + String.valueOf(ringerVol));
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/buttonringer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Ringer Mode"
/>
<Button
android:id="@+id/buttonringerup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ringer VOL+"
/>
<Button
android:id="@+id/buttonringerdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ringer VOL-"
/>
<TextView
android:id="@+id/textringerstate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textringervolume"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


1 則留言:

  1. 感謝您的分享
    順便請教一下
    如果我再調整音量時
    希望能夠有一條BAR,顯示我現在音量大概多大
    那我該怎麼做???
    (或者是和按手機音量建 一樣的音量顯示也OK)

    回覆刪除