Mute audio on ExoPlayer

2019-02-07 22:44发布

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 volume ?

5条回答
时光不老,我们不散
2楼-- · 2019-02-07 23:13

try

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

analogous to this line of code

查看更多
做个烂人
3楼-- · 2019-02-07 23:18

The new way to mute and unmute volume as of version 2.3.1 can be done as follows:

int currentvolume = player.getVolume();

make sure to call the line above after starting the player otherwise you will get a nullpointerexception

to mute volume:

player.setVolume(0f);

to unmute volume:

player.setVolume(currentVolume);
查看更多
淡お忘
4楼-- · 2019-02-07 23:20

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);
查看更多
做自己的国王
5楼-- · 2019-02-07 23:21

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

查看更多
来,给爷笑一个
6楼-- · 2019-02-07 23:25

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);
查看更多
登录 后发表回答