AudioManager的简单使用

R.raw.music为任意音乐
package com.example.audiomanager;


import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {

	private AudioManager aManger;
	private MediaPlayer mPlayer;
	
	private boolean flag=true;//是否设置为静音
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取系统的音频服务
		aManger = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
	}
	
	
	public void myOnClick(View view)
	{
		switch(view.getId())
		{
			case R.id.play:  
				if(mPlayer==null)
				{
					// 初始化MediaPlay ,准备播放音乐
					mPlayer = MediaPlayer.create(MainActivity.this,
							R.raw.earth);
					// 设置是否循环播放
					mPlayer.setLooping(true);
					// 开始播放
					mPlayer.start();
				}
				break;
			case R.id.up:
				// 调大音量,而且显示音量的图形示意
				aManger.adjustStreamVolume(AudioManager.STREAM_MUSIC,
						AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
				break;
			case R.id.down:
				// 调小音量,而且音量的图形示意
				aManger.adjustStreamVolume(AudioManager.STREAM_MUSIC,
						AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
				break;
			case R.id.mute:
				if(flag)
				{
					aManger.setStreamMute(AudioManager.STREAM_MUSIC, flag);
					flag=false;
				}else{
					aManger.setStreamMute(AudioManager.STREAM_MUSIC,flag);
					flag=true;
				}
				break;
			
		}
	}

}



  

你可能感兴趣的:(Android,AudioManage,Android)