Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 控制手機音量的大小 切換聲音的模式

android 控制手機音量的大小 切換聲音的模式

編輯:關於Android編程

(1)程序說明

在android API的AudioManager中,提供了調節手機音量的辦法。

audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
也可以調節手機聲音的模式為震動或者靜音

audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);

audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

(2)布局文件



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

(3)代碼:

package com.liuzuyi.soundmode;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	 private ImageView myimage;
	 private ImageButton downbutton;
	 private ImageButton upbutton;
	 private ImageButton normalbutton;
	 private ImageButton mutebutton;
	 private ImageButton vibratebutton;
	 private ProgressBar myprogress;
	 private AudioManager audioMa;
	 private int volume; 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		audioMa =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
		myimage = (ImageView)findViewById(R.id.myImage);
		myprogress =(ProgressBar)findViewById(R.id.myProgress);
		
		downbutton =(ImageButton)findViewById(R.id.downButton);
		upbutton =(ImageButton)findViewById(R.id.upButton);
		normalbutton=(ImageButton)findViewById(R.id.normalButton);
		mutebutton=(ImageButton)findViewById(R.id.muteButton);
		vibratebutton=(ImageButton)findViewById(R.id.vibrateButton);
		
		volume =audioMa.getStreamVolume(AudioManager.STREAM_RING);
		myprogress.setProgress(volume);
		int mode =audioMa.getRingerMode();
		if(mode == AudioManager.RINGER_MODE_NORMAL ){
			myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
			
		}
		else if(mode == AudioManager.RINGER_MODE_SILENT){
			myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
		}
		else if(mode == AudioManager.RINGER_MODE_VIBRATE){
			myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
		}
		downbutton.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
				volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
				myprogress.setProgress(volume);
				int mode =audioMa.getRingerMode();
				if(mode == AudioManager.RINGER_MODE_NORMAL ){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
					
				}
				else if(mode == AudioManager.RINGER_MODE_SILENT){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
				}
				else if(mode == AudioManager.RINGER_MODE_VIBRATE){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
				}	
				
			}
		});
		
		upbutton.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
				volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
				myprogress.setProgress(volume);
				int mode =audioMa.getRingerMode();
				if(mode == AudioManager.RINGER_MODE_NORMAL ){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
					
				}
				else if(mode == AudioManager.RINGER_MODE_SILENT){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
				}
				else if(mode == AudioManager.RINGER_MODE_VIBRATE){
					myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
				}	
				
			}
		});
		normalbutton.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
			    myprogress.setProgress(volume);
				myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
			}
		});
		mutebutton.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
			    myprogress.setProgress(volume);
				myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
			}
		});
		vibratebutton.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
			    myprogress.setProgress(volume);
				myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
			}
		});
		
	}
	

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved