android - How to manage MediaPlayer.setVolume() + SeekBar to set it + device volume control -
as recommended doc, i'd use setvolume() method control volume of mediaplayer, i'm bit confused how manage it.
i'm setting volume using seekbar, initialized current volume in audiomanager.stream_music stream.
this makes sense when user not modify device volume control, when does, i'm not sure should do, , have several doubts:
how
mediaplayervolume related device volume? max value (1) set insetvolume()correspondmaudiomanager.getstreammaxvolume(audiomanager.stream_music)if it's case, progress value of
seekbarshould reset every time device volume control set, then, what's point of usingmediaplayer.setvolume()instead of audiomanager.setstreamvolume()?what's standard way of using
mediaplayer.setvolume()seekbar?
private final static int max_volume = 100; private audiomanager maudiomanager; private seekbar mvolumecontrol; mvolumecontrol = (seekbar) getactivity().findviewbyid(r.id.volume_control); maudiomanager = (audiomanager) getactivity().getsystemservice(context.audio_service); mvolumecontrol.setmax(max_volume); mvolumecontrol.setprogress(max_volume*maudiomanager.getstreamvolume(audiomanager.stream_music)/maudiomanager.getstreammaxvolume(audiomanager.stream_music)); mvolumecontrol.setonseekbarchangelistener(mvolumecontrolchangelistener); private seekbar.onseekbarchangelistener mvolumecontrolchangelistener = new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { float volume = (float) (1 - (math.log(max_volume - progress) / math.log(max_volume))); mediaplayer.setvolume(volume, volume); } @override public void onstarttrackingtouch(seekbar seekbar) {} @override public void onstoptrackingtouch(seekbar seekbar) { } };
mediaplayer.setvolume sets volume of player percentage of overall volume stream. not affect stream volume. if want set player's volume, set between 0 , 1 (a fractional value representing percentage) , let user control system volume separately using hardware volume controls. hook seekbar, can scale factor of 100 you've done in sample code, since seekbar doesn't support fractional values. oh, , 1 more thing others' benefit (since you've figured out): docs "ui controls should scaled logarithmically", means if set volume without scaling, volume control feel unnatural. basically, halving amplitude of wave not perceived human ear "half volume", need scale logarithmically make feel more natural human ear, you've done in sample code.
if want set system volume given stream (and not given player), can use audiomanager.setstreamvolume value between 0 , value returned audiomanager.getstreammaxvolume. keep in mind that 1 low-resolution volume control - getmaxstreamvolume returns relatively low value - 10 or 15, have few possible positions volume. means controlling volume not smooth if control mediaplayer's volume directly. also, obviously, setting volume way sets entire system , affects applications, may not user wants.
Comments
Post a Comment