Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mute a playing video by VideoView in Android Application

Tags:

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();   } 
like image 601
Vishal Avatar asked Sep 15 '12 06:09

Vishal


People also ask

How to Mute audio in VideoView in Android?

Swipe right or left to find the video clip you would like to change the audio options for. 3. Tap on the 3 dots below the video clip and tap on “Mute” or “Unmute” in the dropdown menu.


1 Answers

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();             }          }  }; 
like image 128
Leonardo Arango Baena Avatar answered Sep 28 '22 02:09

Leonardo Arango Baena