在视频视图中播放视频

时间:2021-05-30 18:59:44

I'm playing a video (from youtube) using a VideoView, like this:

我正在播放一个视频(从youtube)使用视频视图,像这样:

VideoView video = (ViewView) this.findViewById(R.id.youtube_video);
MediaController controllers = new MediaController(this);
controllers.setAnchorView(video);
video.setMediaController(controllers);

video.setVideoURI(videoUri);
video.start()

I would like to be able to mute the video, but so far the only thing I found is:

我想把视频调成静音,但到目前为止,我发现的唯一一件事是:

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);

Which works fine in that the video is muted, but the problem is that all of the streamed music is muted, and if there's another video (or audio) which I want to play it's also muted.

这很好,因为视频是柔和的,但问题是所有的流媒体音乐都是柔和的,如果有另一个视频(或音频),我想播放它也是柔和的。

Is there a way to just target the specific stream to be muted?
Thanks.

是否有一种方法可以将特定的流设置为静音?谢谢。

3 个解决方案

#1


20  

After digging into every possible source of information I managed to find, I came up with the following solution and thought it might benefit others in the future:

在挖掘了所有可能的信息来源后,我想到了下面的解决方案,并认为它将来可能对其他人有益:

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

    public Player(Context context, AttributeSet attributes) {
        super(context, attributes);

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

It seems to working well for me, acting just like a VideoView with mute/unmute functionality.
It's possible to make the setVolume method public so that volume can be controlled outside of the scope of the class, but I just needed mute/unmute to be exposed.

它对我来说似乎很好用,就像一个带静音/消音功能的视频视图。可以将setVolume方法设置为public,以便可以在类的范围之外控制音量,但是我只需要静音/unmute来公开。

#2


1  

put this code in oncreate() and also in onresume() for handle video view in better way...

将此代码放入oncreate()和onresume()中,以便更好地处理视频视图……

  VideoView videoview = (VideoView) findViewById(R.id.videoview);
  videoview.setVideoPath(videopath);
  videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.setVolume(0f, 0f);
                        mp.setLooping(true);
                    }
                });
 videoview.start();

#3


0  

I have a simpler method, by doing this you will have a mute/unmute button (this can be used for audio and videos);

我有一个更简单的方法,通过这样做你将有一个静音/unmute按钮(这个可以用于音频和视频);

//set global variable
//private Button mMuteButton;
//private static int aux = 0;
//private AudioManager mAudioManager;
//set the Mute button on clickListener 

  mMuteButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

            if(aux % 2 == 0){

                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
                aux++;


            } else {

                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
                aux++;

            }


        }
    });

}

}

#1


20  

After digging into every possible source of information I managed to find, I came up with the following solution and thought it might benefit others in the future:

在挖掘了所有可能的信息来源后,我想到了下面的解决方案,并认为它将来可能对其他人有益:

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

    public Player(Context context, AttributeSet attributes) {
        super(context, attributes);

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

It seems to working well for me, acting just like a VideoView with mute/unmute functionality.
It's possible to make the setVolume method public so that volume can be controlled outside of the scope of the class, but I just needed mute/unmute to be exposed.

它对我来说似乎很好用,就像一个带静音/消音功能的视频视图。可以将setVolume方法设置为public,以便可以在类的范围之外控制音量,但是我只需要静音/unmute来公开。

#2


1  

put this code in oncreate() and also in onresume() for handle video view in better way...

将此代码放入oncreate()和onresume()中,以便更好地处理视频视图……

  VideoView videoview = (VideoView) findViewById(R.id.videoview);
  videoview.setVideoPath(videopath);
  videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.setVolume(0f, 0f);
                        mp.setLooping(true);
                    }
                });
 videoview.start();

#3


0  

I have a simpler method, by doing this you will have a mute/unmute button (this can be used for audio and videos);

我有一个更简单的方法,通过这样做你将有一个静音/unmute按钮(这个可以用于音频和视频);

//set global variable
//private Button mMuteButton;
//private static int aux = 0;
//private AudioManager mAudioManager;
//set the Mute button on clickListener 

  mMuteButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

            if(aux % 2 == 0){

                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);
                aux++;


            } else {

                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
                aux++;

            }


        }
    });

}

}