2

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

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

现在,如何解决它?

4

2 回答 2

3

您可以像这样自定义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);
        }
}
于 2019-03-10T13:09:20.053 回答
1

您需要调用您想要使用VideoView的MediaPlayer.OnPreparedListenerMediaPlayer.OnCompletionListener。然后您可以公开setVolume方法,以便可以在类范围之外控制音量。以下 3 将解决这些问题。

于 2013-06-16T12:43:52.323 回答