java 在 VideoView 中静音视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13394876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Muting a video in a VideoView
提问by Nitzan Tomer
I'm playing a video (from youtube) using a VideoView, like this:
我正在使用 VideoView 播放视频(来自 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.
有没有办法只针对要静音的特定流?
谢谢。
回答by Nitzan Tomer
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 VideoViewwith mute/unmute functionality.
It's possible to make the setVolumemethod public so that volume can be controlled outside of the scope of the class, but I just needed mute/unmute to be exposed.
它似乎对我来说效果很好,就像具有静音/取消静音功能的VideoView一样。
可以公开setVolume方法,以便可以在类范围之外控制音量,但我只需要静音/取消静音即可公开。
回答by Manthan Patel
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();
回答by Juan Hurtado
I have a simpler method, by doing this you will have a mute/unmute button (this can be used for audio and videos);
我有一个更简单的方法,通过这样做,您将拥有一个静音/取消静音按钮(可用于音频和视频);
//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++;
}
}
});
}
}