13

I'm playing a video (from youtube) using a VideoView, like this:

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 be able to mute the video, but so far the only thing I found is:

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

Which works fine in that the video is muted, but the problem is that all of the streamed music is muted, and if there's another video (or audio) which I want to play it's also muted.

Is there a way to just target the specific stream to be muted?
Thanks.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • 1
    Maybe it can help you : http://stackoverflow.com/questions/12435275/mute-a-playing-video-by-videoview-in-android-application – Bertrand Hieronymus Nov 15 '12 at 09:56
  • Ah, yes, but that suggests the use of *MediaPlayer* instead of the *VideoView*, something I would like to avoid if possible. – Nitzan Tomer Nov 15 '12 at 10:00
  • Currently other than using MediaPlayer or AudioManager i dont think this is possible, VideoView doesnt provide any mute options which is kinda strange. So those are your two options or you could try to create a custom VideoView & add the mute functionality ! – Adnan Mulla Nov 15 '12 at 11:50
  • I managed to come up with a solution that works for me, I posted it as an answer. Thanks. – Nitzan Tomer Nov 15 '12 at 13:04

4 Answers4

27

After digging into every possible source of information I managed to find, I came up with the following solution and thought it might 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 working well for me, acting just like a VideoView with mute/unmute functionality.
It's possible to make the setVolume method public so that volume can be controlled outside of the scope of the class, but I just needed mute/unmute to be exposed.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
14

put this code in oncreate() and also in onresume() for handle video view in better 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();
Manthan Patel
  • 1,784
  • 19
  • 23
1

If you are coding in kotlin, Just use this line of code

videoView.setOnPreparedListener { video->
            video.setVolume(0f,0f)
        }
Khaled Saifullah
  • 2,279
  • 3
  • 25
  • 26
0

I have a simpler method, by doing this you will have a mute/unmute button (this can be used for audio and videos);

//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++;

            }


        }
    });

}

Juan Hurtado
  • 358
  • 1
  • 7
  • 1
    This is not the correct solution. This will mute everything using STREAM_MUSIC not just the video. If your activity only plays video this might be ok but it will have side affects I think Nitzan wants to avoid. – Keith Loughnane Mar 13 '17 at 11:52