Disabling videos in VideoView

I am playing a video (from youtube) using a VideoView, for example:

VideoView video = (ViewView) this.findViewById(R.id.youtube_video); MediaController controllers = new MediaController(this); controllers.setAnchorView(video); video.setMediaController(controllers); video.setVideoURI(videoUri); video.start() 

I would like to disable the video, but so far the only thing I have found:

 AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); 

Which works great in that the video is disabled, but the problem is that all streaming music is disabled, and if there is another video (or audio) that I want to play, it also turns off.

Is there a way to just disable a specific thread?
Thanks.

+9
source share
4 answers

After searching for all possible sources of information, I managed to find it, I came up with the following solution and thought that it could benefit others in the future:

 public class Player extends VideoView implements OnPreparedListener, OnCompletionListener, OnErrorListener { private MediaPlayer mediaPlayer; public Player(Context context, AttributeSet attributes) { super(context, attributes); this.setOnPreparedListener(this); this.setOnCompletionListener(this); this.setOnErrorListener(this); } @Override public void onPrepared(MediaPlayer mediaPlayer) { this.mediaPlayer = mediaPlayer; } @Override public boolean onError(MediaPlayer mediaPlayer, int what, int extra) { ... } @Override public void onCompletion(MediaPlayer mediaPlayer) { ... } public void mute() { this.setVolume(0); } public void unmute() { this.setVolume(100); } private void setVolume(int amount) { final int max = 100; final double numerator = max - amount > 0 ? Math.log(max - amount) : 0; final float volume = (float) (1 - (numerator / Math.log(max))); this.mediaPlayer.setVolume(volume, volume); } } 

It seems to work well for me, acting just like a VideoView with the mute / unmute function.
This allows you to make the setVolume method publicly available so that the volume can be controlled outside the scope of the class, but I just needed to turn off the sound / enable the sound.

+21
source

put this code in oncreate () as well as in onresume () to watch the video in the best way ...

  VideoView videoview = (VideoView) findViewById(R.id.videoview); videoview.setVideoPath(videopath); videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setVolume(0f, 0f); mp.setLooping(true); } }); videoview.start(); 
+3
source

I have an easier way: in this case you will have a mute / unmute button (this can be used for audio and video);

 //set global variable //private Button mMuteButton; //private static int aux = 0; //private AudioManager mAudioManager; //set the Mute button on clickListener mMuteButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); if(aux % 2 == 0){ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0); aux++; } else { mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); aux++; } } }); 

}

0
source

@Manthan Patel Answer works for me, thanks

0
source

All Articles