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 ?
Get the current volume:
int currentvolume = player.getVolume();
Mute: player.setVolume(0f);
Unmute: player.setVolume(currentVolume);
I found two ways to achieve it by editing DemoPlayer
from ExoPlayer.
Basicly, you need to get the audioTrackRenderer
which is a ExoPlayerComponent
and send message to it.
So :
Add audioRenderer
member and set it in onRenderers
:
// Complete preparation.
this.videoRenderer = renderers[TYPE_VIDEO];
this.audioRenderer = renderers[TYPE_AUDIO];
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);
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);