Farzad
Farzad

Reputation: 212

How To Mute Sound Video in VideoView Android

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?

Upvotes: 2

Views: 11413

Answers (2)

Farzad
Farzad

Reputation: 212

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);
        }
}

Upvotes: 2

ridoy
ridoy

Reputation: 6332

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..

Upvotes: 1

Related Questions