Disabling sound in a video

11,661

Solution 1

The best you can do is use AudioManager to mute the music stream at the beginning of the cut scene and unmute it once it is done: VideoView does not provide independent mute controls.

Solution 2

The AudioManager is not a good option always, because you mute all the system...

if you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call setVolume(0f, 0f); function to set the volume to 0.

Do this:

@Override

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_video);

  VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  videoView.setMediaController(mc);
  String _path = "/mnt/sdcard/Movies/video5.mp4";

  videoView.setVideoPath(_path);
  videoView.setOnPreparedListener(PreparedListener);

  videoView.requestFocus();

  //Dont start your video here
  //videoView.start();


}

MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

     @Override
     public void onPrepared(MediaPlayer m) {
         try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m.setVolume(0f, 0f);
                m.setLooping(false);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }    
     }
 };
Share:
11,661
Athos
Author by

Athos

I want to learn programming! :D

Updated on June 26, 2022

Comments

  • Athos
    Athos almost 2 years

    Hi I'm new to android development and working on a game. I currently have an opening cutsceen which is a videoview, but I want to mute the the sound, if the user selects mute music from the preference menu. The problem is I don't know how to mute the music in a videoview without actually turning off the video entirely!

  • LamaTo
    LamaTo over 7 years
    This isn't a good option for low API, Media Controller request 21 and a lot of app's using lower API then this.
  • Jaydev
    Jaydev over 7 years
    @LamaTo - What do you mean by "low API" and "request 21"?
  • LamaTo
    LamaTo over 7 years
    @Jaydev - API level 21 is for android 5.0 and above. see this for more info
  • rharter
    rharter about 7 years
    @LamaTo I believe you're confusing the MediaController being used. This is android.widget.MediaController, which is available since API 1, not android.media.session.MediaController, which was added in API 21. That's the problem with poor naming :/