Mute audio on ExoPlayer

后端 未结 7 1624
走了就别回头了
走了就别回头了 2020-12-09 02:13

I\'m using Google new MediaPlayer named ExoPlayer and cannot find a way to mute the sound

Is there an easy way to mute audio track on Google ExoPlayer ? Or changing

相关标签:
7条回答
  • 2020-12-09 02:29

    Just simply use player.setVolume(0) will silent the video.

    0 讨论(0)
  • 2020-12-09 02:30

    In ExoPlayer 2.x.x:

    get the current volume:

    int currentVolume = player..getAudioComponent().getVolume();
    

    mute:

    player.getAudioComponent().setVolume(0f);
    

    unmute:

    player.getAudioComponent().setVolume(currentVolume);
    
    0 讨论(0)
  • 2020-12-09 02:33

    try

    player.setSelectedTrack(DemoPlayer.TYPE_AUDIO, DemoPlayer.TRACK_DISABLED);
    

    analogous to this line of code

    0 讨论(0)
  • 2020-12-09 02:35

    Easily you can do it with ExoPlayer:

    Java Code:

    float currentVolume = player.getVolume();
    if (currentVolume == 0f) {
          player.setVolume(1f);
    } else {
          player.setVolume(0f);
    }
    

    Kotlin Code:

    val curentVol = player?.volume
    if (curentVol == 0f) {
         player?.volume = 1f
    } else {
         player?.volume = 0f
    }
    
    0 讨论(0)
  • 2020-12-09 02:35

    I will recommend get the current volume first and then mute it. When you will unmute you can give user same volume.

    float currentvolume;
    currentvolume = player.getVolume();
    player.setVolume(0.0f);
    
    0 讨论(0)
  • 2020-12-09 02:38

    Exoplayer 2.x.x

    Get the current volume: int currentvolume = player.getVolume();
    Mute: player.setVolume(0f);
    Unmute: player.setVolume(currentVolume);


    Exoplayer 1.x.x

    I found two ways to achieve it by editing DemoPlayer from ExoPlayer.

    Good one :

    Basicly, you need to get the audioTrackRenderer which is a ExoPlayerComponent and send message to it. So :

    1. Add audioRenderer member and set it in onRenderers:

      // Complete preparation.  
      this.videoRenderer = renderers[TYPE_VIDEO];  
      this.audioRenderer = renderers[TYPE_AUDIO];  
      
    2. Add public method :

      public void setMute(boolean toMute){
          if(toMute){
              player.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 0f);
          } else {
              player.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 1f);
          }
      }
      

    Usage :
    mute : player.setMute(true);
    unmute : player.setMute(false);

    The other one :

    This is not a good solution has the player will need to rebuffer when unmuting.
    Consist of changing the audio track to an empty one:

    // mute
    player.selectTrack(FullPlayer.TYPE_AUDIO, ExoPlayer.TRACK_DISABLED);
    
    // Unmute
    player.selectTrack(FullPlayer.TYPE_AUDIO, ExoPlayer.TRACK_DEFAULT);
    
    0 讨论(0)
提交回复
热议问题