views:

36

answers:

2

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 mediaController allowing the user to stop and play, I have "touch" events controlling this.

UPDATE: I have a menu that I have added a "mute" icon to. Now I am trying to figure out how to add the mute to this button. I am reading some info on from the Android AudioManager, in particular the setStreamMute. Here is what the API's say:

  public void  setStreamMute  (int streamType, boolean state)

Since: API Level 1

Mute or unmute an audio stream.

The mute command is protected against client process death: if a process with an active mute request on a stream dies, this stream will be unmuted automatically.

The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received.

For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate.

This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application. Parameters streamType The stream to be muted/unmuted. state The required mute state: true for mute ON, false for mute OFF

+1  A: 

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:

public void mute() {
  AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
  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.

Wireless Designs
That makes sense, but I am confused how to call these from one of my menu buttons. case R.id.main_menu_mute:
taraloca
Okay...I did figure out the first press of the button simply by placing the call to the mute() inside my "case", but now I need to figure out how to unmute it with another click.
taraloca
A: 

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:

private AudioManager mAm;
private boolean mIsMute;

// Audio mgr
mAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mIsMute = false;

public void isMute() {

      if(mIsMute){    
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
          mIsMute = false;

      }else{
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
          mIsMute = true;
      }
    }

And then inside my case:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){

    // Mute
    case R.id.main_menu_mute:
        isMute();
        break;

    .........
    }

    return super.onOptionsItemSelected(item);
}
taraloca