Vishal
Vishal

Reputation: 693

Mute a playing video by VideoView in Android Application

I want to mute a playing Video by VideoView in my Android Application. I couldn't find any method to do so in VideoView Class. Any idea how to do this?

I have found a method "setVolume" in MediaPlayer Class, But I am unable to find any working code to play video by MediaPlayer class. So I believe I can set volume 0 by this method.

Therefore I am looking for any working code to play video using MediaPlayer Class or how to control volume using VideoView Class.

Below is the code for playing video using VideoView , which I am using.

@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.requestFocus();
    videoView.start();


}

Upvotes: 24

Views: 33949

Answers (3)

Leonardo Arango Baena
Leonardo Arango Baena

Reputation: 880

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 MediaPlayer.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();
            }    
     }
 };

Upvotes: 31

Ahmad Arslan
Ahmad Arslan

Reputation: 4528

videoview.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            mp.setVolume(0, 0);
        }
    });

Upvotes: 29

Vishal
Vishal

Reputation: 693

I have done this using MediaPlayer class. I have used setVolume function of MediaPlayer class to set the volume to 0. also I have realised that dont use AudioManager class, because using AudioManager if a set volume to 0, then it set volume to 0 for all the instance of MediaPlayer and VideoView. But if you will use setVolume() method of MediaPlayer then it will just Mute the volume of that instance only.

Also set volume to 0 is bot easy using VideoView because VideoView is a wrapper over MediaPlayer class and just allow to access few function of MediaPlayer. Also I have read on some blog that though you can reference MediaPlayer instance using VideoView instances, but its very complex and its not recommended to do so. Hope this would be helpful to other new readers how try to do similar things.

Upvotes: 4

Related Questions