在 Android 应用程序中通过 VideoView 将正在播放的视频静音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12435275/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:39:17  来源:igfitidea点击:

Mute a playing video by VideoView in Android Application

androidvideo

提问by Vishal

I want to mute a playing Video by VideoView in my Android Application. I couldn't find any method to do so in VideoView Class. Any idea how to do this?

我想在我的 Android 应用程序中通过 VideoView 将正在播放的视频静音。我在 VideoView 类中找不到这样做的任何方法。知道如何做到这一点吗?

I have found a method "setVolume" in MediaPlayer Class, But I am unable to find any working code to play video by MediaPlayer class. So I believe I can set volume 0 by this method.

我在 MediaPlayer 类中找到了一个方法“setVolume”,但是我找不到任何可以通过 MediaPlayer 类播放视频的工作代码。所以我相信我可以通过这种方法设置音量0。

Therefore I am looking for any working code to play video using MediaPlayer Class or how to control volume using VideoView Class.

因此,我正在寻找任何工作代码来使用 MediaPlayer 类播放视频或如何使用 VideoView 类控制音量。

Below is the code for playing video using VideoView , which I am using.

下面是使用 VideoView 播放视频的代码,我正在使用它。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
    MediaController mc = new MediaController(this);
    mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    videoView.setMediaController(mc);
    String _path = "/mnt/sdcard/Movies/video5.mp4";

    videoView.setVideoPath(_path);

    videoView.requestFocus();
    videoView.start();


}

采纳答案by Vishal

I have done this using MediaPlayer class. I have used setVolume function of MediaPlayer class to set the volume to 0. also I have realised that dont use AudioManager class, because using AudioManager if a set volume to 0, then it set volume to 0 for all the instance of MediaPlayer and VideoView. But if you will use setVolume() method of MediaPlayer then it will just Mute the volume of that instance only.

我已经使用 MediaPlayer 类完成了这项工作。我已经使用 MediaPlayer 类的 setVolume 函数将音量设置为 0。我也意识到不要使用 AudioManager 类,因为使用 AudioManager 如果将音量设置为 0,那么它会将 MediaPlayer 和 VideoView 的所有实例的音量设置为 0。但是,如果您将使用 MediaPlayer 的 setVolume() 方法,那么它只会将该实例的音量静音。

Also set volume to 0 is bot easy using VideoView because VideoView is a wrapper over MediaPlayer class and just allow to access few function of MediaPlayer. Also I have read on some blog that though you can reference MediaPlayer instance using VideoView instances, but its very complex and its not recommended to do so. Hope this would be helpful to other new readers how try to do similar things.

使用 VideoView 将音量设置为 0 也很容易,因为 VideoView 是 MediaPlayer 类的包装器,只允许访问 MediaPlayer 的一些功能。另外我在一些博客上读到,虽然您可以使用 VideoView 实例引用 MediaPlayer 实例,但它非常复杂,不建议这样做。希望这对其他新读者如何尝试做类似的事情有所帮助。

回答by Leonardo Arango Baena

If you want to get access to the MediaPlayerof a VideoViewyou have to call MediaPlayer.OnPreparedListenerand MediaPlayer.OnCompletionListener, then you can call MediaPlayer.setVolume(0f, 0f);function to set the volume to 0.

如果您想访问MediaPlayera 的,则VideoView必须调用MediaPlayer.OnPreparedListenerMediaPlayer.OnCompletionListener,然后您可以调用MediaPlayer.setVolume(0f, 0f);函数将音量设置为 0。

Do this:

做这个:

@Override

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_video);

  VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  videoView.setMediaController(mc);
  String _path = "/mnt/sdcard/Movies/video5.mp4";

  videoView.setVideoPath(_path);
  videoView.setOnPreparedListener(PreparedListener);

  videoView.requestFocus();

  //Dont start your video here
  //videoView.start();


}

MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

     @Override
     public void onPrepared(MediaPlayer m) {
         try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m.setVolume(0f, 0f);
                m.setLooping(false);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }    
     }
 };

回答by Ahmad Arslan

videoview.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            mp.setVolume(0, 0);
        }
    });