Android App中使用AudioManager类来编写音频播放器
手机都有声音模式,声音、静音还有震动,甚至震动加声音兼备,这些都是手机的基本功能。在Android手机中,我们同样可以通过Android的SDK提供的声音管理接口来管理手机声音模式以及调整声音大小,这就是Android中AudioManager的使用。
AudioManager类位于android.Media包中,该类提供访问控制音量和钤声模式的操作
以下分别是AudioManager设置声音模式和调整声音大小的方法。
如何获取声音管理器:
AudioManageraudioManager=(AudioManager)getSystemService(AUDIO_SERVICE);
里面主要的方法:
A、设置声音模式
//声音模式 AudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); //静音模式 AudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //震动模式 AudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
B、调整声音大小
//减少声音音量 AudioManager.adjustVolume(AudioManager.ADJUST_LOWER,0); //调大声音音量 AudioManager.adjustVolume(AudioManager.ADJUST_RAISE,0); (当传入的第一个参数为AudioManager.ADJUST_LOWER时,可将音量调小一个单位,传入AudioManager.ADJUST_RAISE时,则可以将音量调大一个单位。)
C、getMode()获取音频模式
D、getRingerMode()获取铃声震动模式
publicvoidsetStreamMute(intstreamType,booleanstate)
静音或不静音音频流:
静音命令被保护以免客户端进程死亡:若具有流上的活动静音请求的进程死亡,这个流会自动取消静音。
对于给定的流,静音请求是累计的:AudioManager会从一个或多个客户端接收数个静音请求,只有当接收到相同数目的取消静音请求时流才会取消静音。
为了更好的用户体验,应该程序必须在onPause()中取消已静音流,若合适在onResume()中再次静音
这个方法只能用于代替音频设置的平台范围管理应用程序或主要电话应用程序。
调用示例:
importandroid.app.Activity; importandroid.app.Service; importandroid.media.AudioManager; importandroid.media.MediaPlayer; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.CompoundButton; importandroid.widget.CompoundButton.OnCheckedChangeListener; importandroid.widget.ToggleButton; publicclassAutoActivityextendsActivity{ Buttonplay,up,down,stop; ToggleButtonmute; AudioManageraManager; MediaPlayermPlayer; booleanflag=true; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto); aManager=(AudioManager)getSystemService(Service.AUDIO_SERVICE); play=(Button)findViewById(R.id.playBtn); up=(Button)findViewById(R.id.upBtn); down=(Button)findViewById(R.id.downBtn); stop=(Button)findViewById(R.id.stopBtn); mute=(ToggleButton)findViewById(R.id.silenceBtn); //初始化MediaPlayer对象,准备播放音乐 mPlayer=MediaPlayer.create(AutoActivity.this, R.raw.love); play.setOnClickListener(newOnClickListener(){ //音乐播放和暂停 @Override publicvoidonClick(Viewv){ //设置循环播放 if(flag){ play.setText("暂停"); mPlayer.setLooping(true);//音乐的循环播放 mPlayer.start(); flag=false; }else{ play.setText("播放"); mPlayer.pause(); flag=true; } } }); up.setOnClickListener(newOnClickListener(){ //提高音量 @Override publicvoidonClick(Viewv){ //指定调节音乐的音频,增大音量,而现实音量图形示意 aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,AudioManager.FLAG_SHOW_UI); } }); down.setOnClickListener(newOnClickListener(){ //降低音量 @Override publicvoidonClick(Viewv){ //指定调节音乐的音频,降低音量,而现实音量图形示意 aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI); } }); stop.setOnClickListener(newOnClickListener(){ //停止音乐 @Override publicvoidonClick(Viewv){ mPlayer.stop(); play.setText("播放"); flag=true; mPlayer=MediaPlayer.create(AutoActivity.this, R.raw.love); } }); mute.setOnCheckedChangeListener(newOnCheckedChangeListener(){ //静音功能 @Override publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){ aManager.setStreamMute(AudioManager.STREAM_MUSIC,isChecked); } }); } }
界面布局文件,activity_auto.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="360dp"> <TableRow> <Button android:id="@+id/playBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="播放"/> <Button android:id="@+id/stopBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止"/> </TableRow> <TableRow> <Button android:id="@+id/upBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="VOL+"/> <Button android:id="@+id/downBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="VOL-"/> <ToggleButton android:id="@+id/silenceBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="静音" android:textColor="#ff0000"/> </TableRow> </TableLayout> </LinearLayout>
程序比较简单,只有几个简单的功能按钮,但是理解用法用该够了~
MediaPlayer的使用方式和方法,具体来看:
1)如何获得MediaPlayer实例:
可以使用直接new的方式:
MediaPlayermp=newMediaPlayer();
也可以使用create的方式,如:
MediaPlayermp=MediaPlayer.create(this,R.raw.test);//这时就不用调用setDataSource了
2)如何设置要播放的文件:
MediaPlayer要播放的文件主要包括3个来源:
a.用户在应用中事先自带的resource资源
例如:
MediaPlayer.create(this,R.raw.test);
b.存储在SD卡或其他文件路径下的媒体文件
例如:
mp.setDataSource("/sdcard/test.mp3");
c.网络上的媒体文件
例如:
mp.setDataSource("http://www.citynorth.cn/music/confucius.mp3");
MediaPlayer的setDataSource一共四个方法:
- setDataSource(Stringpath)
- setDataSource(FileDescriptorfd)
- setDataSource(Contextcontext,Uriuri)
- setDataSource(FileDescriptorfd,longoffset,longlength)
3)对播放器的主要控制方法:
Android通过控制播放器的状态的方式来控制媒体文件的播放,其中:
prepare()和prepareAsync() 提供了同步和异步两种方式设置播放器进入prepare状态,需要注意的是,如果MediaPlayer实例是由create方法创建的,那么第一次启动播放前不需要再调用prepare()了,因为create方法里已经调用过了。
start()是真正启动文件播放的方法,
pause()和stop()比较简单,起到暂停和停止播放的作用,
seekTo()是定位方法,可以让播放器从指定的位置开始播放,需要注意的是该方法是个异步方法,也就是说该方法返回时并不意味着定位完成,尤其是播放的网络文件,真正定位完成时会触发OnSeekComplete.onSeekComplete(),如果需要是可以调用setOnSeekCompleteListener(OnSeekCompleteListener)设置监听器来处理的。
release()可以释放播放器占用的资源,一旦确定不再使用播放器时应当尽早调用它释放资源。
reset()可以使播放器从Error状态中恢复过来,重新会到Idle状态。
4)设置播放器的监听器:
MediaPlayer提供了一些设置不同监听器的方法来更好地对播放器的工作状态进行监听,以期及时处理各种情况,
如:setOnCompletionListener(MediaPlayer.OnCompletionListenerlistener)、
setOnErrorListener(MediaPlayer.OnErrorListenerlistener)等,设置播放器时需要考虑到播放器可能出现的情况设置好监听和处理逻辑,以保持播放器的健壮性。