Supplying Option To User To Play Video With Or Without Audio
I am using VideoView to play an mp4 video. I would like to give the user the option of watching this video with sound or mute the sound if he/she chooses. I do not use the mediaC
Solution 1:
Use the AudioManager service to mute and unmute just the stream related to your video. From the method(s) you have declared to respond to the user touch events, call methods like:
publicvoidmute() {
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
publicvoidunmute() {
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
This will leave the other streams (notification, alarm, etc.) active so you aren't silencing the whole device just to mute the video.
Also, if you need to suggest to your Activity which stream it should be pushing the audio through you can call Activity.setVolumeControlStream(AudioManager.STREAM_MUSIC)
to tie your Activity's window to that stream.
Solution 2:
I was able to implement my desire to have a mute button contained in a menu button. Each time the user interacts with the button, the video either mutes or unmutes. Here is the code:
privateAudioManager mAm;
privateboolean mIsMute;
// Audio mgr
mAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mIsMute = false;
publicvoidisMute() {
if(mIsMute){
mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
mIsMute = false;
}else{
mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
mIsMute = true;
}
}
And then inside my case:
publicbooleanonOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
// Mutecase R.id.main_menu_mute:
isMute();
break;
.........
}
returnsuper.onOptionsItemSelected(item);
}
Post a Comment for "Supplying Option To User To Play Video With Or Without Audio"