如何在VideoView Android中静音视频

问题描述 投票:1回答:2

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

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

现在,如何解决它?

android video-streaming
2个回答
1
投票

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


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);
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.