How To Mute Sound Video in VideoView Android

10,417

Solution 1

You can custom VideoView like that

    public class VideoPlayer extends VideoView implements OnPreparedListener, OnCompletionListener, OnErrorListener {
        private MediaPlayer mediaPlayer;

        public Player(Context context, AttributeSet attributes) {
            super(context, attributes);
           //init player
            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;//change 100 to zeo
            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);
        }
}

Solution 2

You need to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener of you want to use VideoView .Then you can make setVolume method public so that volume can be controlled outside of the scope of the class.Below 3 will solve these..

Share:
10,417
Farzad
Author by

Farzad

Updated on June 04, 2022

Comments

  • Farzad
    Farzad almost 2 years

    I want Mute Sound of video and use Videoview for playing video

       _player.setVideoURI("/sdcard/Movie/byern.mp4");
       _player.start();
    

    Now,How can Resolve it?