Mute a playing video by VideoView in Android Application

前端 未结 3 378
梦谈多话
梦谈多话 2020-12-03 13:26

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?

I have found

相关标签:
3条回答
  • 2020-12-03 14:06

    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.

    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.

    0 讨论(0)
  • 2020-12-03 14:15

    If you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call MediaPlayer.setVolume(0f, 0f); function to set the volume to 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();
                }    
         }
     };
    
    0 讨论(0)
  • 2020-12-03 14:24
    videoview.setOnPreparedListener(new OnPreparedListener() {
    
            @Override
            public void onPrepared(MediaPlayer mp) {
    
                mp.setVolume(0, 0);
            }
        });
    
    0 讨论(0)
提交回复
热议问题