2011.08.15(2)——— android audioManager解决MediaPlayer AudioTrack 调节音量问题

    2011.08.15(2)——— android audiomanager解决mediaplayer audiotrack 调节音量问题

参考: http://www.pocketdigi.com/20110717/398.html

在听筒模式下

am.setspeakerphoneon(false);setvolumecontrolstream(audiomanager.stream_voice_call);am.setmode(audiomanager.mode_in_call);


我用mediaplayer audiotrack调节音量总是失败
at.setstereovolume(vol, vol);player.setvolume(vol,vol);


后来 决定用audiomanager来调节音量

audiomanager可以修改系统android系统的音量
下面介绍几个audiomanager的几个音量调整方面的方法.
首先是得到audiomanager实例:


audiomanager am=(audiomanager)getsystemservice(context.audio_service);


调整音量方法有两种,一种是渐进式,即像手动按音量键一样,一步一步增加或减少,另一种是直接设置音量值.
1、渐进式
public void adjuststreamvolume (int streamtype, int direction, int flags)am.adjuststreamvolume (audiomanager.stream_music, audiomanager.adjust_raise, audiomanager.flag_show_ui);

解释一下三个参数

第一个streamtype是需要调整音量的类型,这里设的是媒体音量,可以是:stream_alarm 警报stream_music 音乐回放即媒体音量stream_notification 窗口顶部状态栏notification,stream_ring 铃声stream_system 系统stream_voice_call 通话stream_dtmf 双音多频,不是很明白什么东西第二个direction,是调整的方向,增加或减少,可以是:adjust_lower 降低音量adjust_raise 升高音量adjust_same 保持不变,这个主要用于向用户展示当前的音量第三个flags是一些附加参数,只介绍两个常用的flag_play_sound 调整音量时播放声音flag_show_ui 调整时显示音量条,就是按音量键出现的那个


2、直接设置音量值的方法:
public void setstreamvolume (int streamtype, int index, int flags)am.setstreamvolume(audiomanager.stream_music, am.getstreammaxvolume(audiomanager.stream_music), audiomanager.flag_play_sound);am.getstreammaxvolume(audiomanager.stream_voice_call);//得到听筒模式的最大值am.getstreamvolume(audiomanager.stream_voice_call);//得到听筒模式的当前值

第一个和第三个参数与上面的相同
第二个参数是一个音量的int值,getstreammaxvolume(int streamtype)得到的是该类型音量的最大值,可以根据这个值计算你需要的音量,我这里直接调到最大.


最后我的代码:

package com.lp;import java.io.file;import java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.ioexception;import java.io.inputstream;import android.app.activity;import android.content.context;import android.media.audioformat;import android.media.audiomanager;import android.media.audiotrack;import android.os.bundle;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;import android.widget.seekbar;/** * audiotrack 播放音频 如wav格式 * 并允许调节音量 * @author administrator * */public class mainactivity5 extends activity {	private button play;	private button stop;	private seekbar soundvalue;		private audiotrack at;	private audiomanager am;    @override    public void oncreate(bundle savedinstancestate) {        super.oncreate(savedinstancestate);        setcontentview(r.layout.main_sk);        am = (audiomanager)getsystemservice(context.audio_service);        play = (button)findviewbyid(r.id.main_sk_play);        stop = (button)findviewbyid(r.id.main_sk_stop);        soundvalue = (seekbar)findviewbyid(r.id.skbvolume);        setvolumecontrolstream(audiomanager.stream_voice_call);        play.setonclicklistener(new onclicklistener() {			@override			public void onclick(view v) {								if(am.isspeakerphoneon()){					am.setspeakerphoneon(false);				}				//setvolumecontrolstream(audiomanager.stream_voice_call);				am.setmode(audiomanager.mode_in_call);				system.out.println(am.getstreammaxvolume(audiomanager.stream_voice_call));				system.out.println("&&&&&&&&&&&&&");				system.out.println(am.getstreamvolume(audiomanager.stream_voice_call));				//am.setstreamvolume(streamtype, index, flags)								int buffersizeinbytes = audiotrack.getminbuffersize(44100, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit);								if(at==null){					at = new audiotrack(audiomanager.stream_voice_call, 44100, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit, buffersizeinbytes, audiotrack.mode_stream);					system.out.println("22222");					//at.setstereovolume(100f, 100f);					at.setstereovolume(0.7f, 0.7f);//设置当前音量大小					new audiotrackthread().start();				}else{					if(at.getplaystate()==audiotrack.playstate_playing){						system.out.println("111111111");					}else{						system.out.println("33333");						at = new audiotrack(audiomanager.stream_voice_call, 44100, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit, buffersizeinbytes, audiotrack.mode_stream);						new audiotrackthread().start();					}				}							}        });                stop.setonclicklistener(new onclicklistener() {						@override			public void onclick(view v) {				if(at.getplaystate()==audiotrack.playstate_playing){					try{						at.stop();					}catch (illegalstateexception e)					{						e.printstacktrace();					}					at.release();					am.setmode(audiomanager.mode_normal);				}			}   		});        //        soundvalue.setmax(100);//音量调节的极限//        soundvalue.setprogress(70);//设置seekbar的位置值        soundvalue.setmax(am.getstreammaxvolume(audiomanager.stream_voice_call));        soundvalue.setprogress(am.getstreamvolume(audiomanager.stream_voice_call));				soundvalue.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() {						@override			public void onstoptrackingtouch(seekbar seekbar) {//				float vol=(float)(seekbar.getprogress())/(float)(seekbar.getmax());//				system.out.println(vol);//				at.setstereovolume(vol, vol);//设置音量				am.setstreamvolume(audiomanager.stream_voice_call, seekbar.getprogress(), audiomanager.flag_play_sound);			}						@override			public void onstarttrackingtouch(seekbar seekbar) {				// todo auto-generated method stub			}						@override			public void onprogresschanged(seekbar seekbar, int progress,					boolean fromuser) {				// todo auto-generated method stub			}		});    }        class audiotrackthread extends thread{  		@override		public void run() {			byte[] out_bytes = new byte[44100];						inputstream is = getresources().openrawresource(r.raw.start);    			int length ;			try{				at.play();			}catch (illegalstateexception e)			{				e.printstacktrace();			}			try {				while((length = is.read(out_bytes))!=-1){					//system.out.println(length);					at.write(out_bytes, 0, length);				}			} catch (ioexception e) {				e.printstacktrace();			}			if(at.getplaystate()==audiotrack.playstate_playing){				try{					at.stop();				}catch (illegalstateexception e)				{					e.printstacktrace();				}				at.release();				am.setmode(audiomanager.mode_normal);			}		}    	    }				}



当然 权限
<uses-permission android:name="android.permission.modify_audio_settings" />    <uses-permission android:name="android.permission.write_external_storage" /> 	<uses-permission android:name="android.permission.record_audio" />

 

你可能感兴趣的:(java,工作)