如何在VideoView Android中静音视频

时间:2013-06-16 11:43:41

标签: android video-streaming

我想要静音视频,并使用Videoview播放视频

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

现在,如何解决它?

2 个答案:

答案 0 :(得分:1)

您需要调用想要使用 VideoView MediaPlayer.OnPreparedListener MediaPlayer.OnCompletionListener 。然后您可以调用 setVolume 方法public使得卷可以在类的范围之外进行控制。下面3将解决这些问题。

答案 1 :(得分:0)

您可以像这样自定义 VideoView

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