48

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 ?

Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119

9 Answers9

66

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);
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
  • Great solution! I was using "selectTrack" way but the video stream rebuffed every time I selected different audio track. – Repminister Jul 08 '15 at 08:15
  • 1
    In ExoPlayer 2.5 you can do the track selection solution. "In ExoPlayer 2.5 this rebuffering has been eliminated, allowing audio and text track selections to be made in a seamless way." https://medium.com/google-exoplayer/exoplayer-2-5-whats-new-b508c0ab606f – TalkLittle Aug 07 '17 at 23:32
  • Is it possible for the ExoPlayer to play something at a volume that is independent from any system volume level? – android developer Dec 30 '18 at 09:08
  • @androiddeveloper I doubt that this is not an expected behavior for the User. As stated in the doc: `Android uses separate audio streams for playing music, alarms, notifications, the incoming call ringer, system sounds, in-call volume, and DTMF tones. This allows users to control the volume of each stream independently.` Plus: `Unless your app is an alarm clock, you should play audio using the STREAM_MUSIC stream.` Ref: https://developer.android.com/guide/topics/media-apps/volume-and-earphones – Hugo Gresse Jan 02 '19 at 08:49
  • @HugoGresse Sometimes it's important to play a sound that the user will hear. I wanted to avoid changing global volume levels just for that. – android developer Jan 02 '19 at 10:30
  • probably worth another stackoverflow search like https://stackoverflow.com/questions/4071149/override-silent-mode-and-or-media-volume – Hugo Gresse Jan 02 '19 at 15:51
  • There is a problem when an incoming call occurs, on some devices volume restores to default but ExoPlayer tells me volume is zero, which is false. https://github.com/google/ExoPlayer/issues/3923 – Max May 20 '20 at 10:20
25

The new way to mute and unmute volume as of version 2.X.X 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);
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
Khoosham
  • 416
  • 5
  • 8
9

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
}
Ashav Kothari
  • 301
  • 3
  • 7
5

In ExoPlayer 2.x.x:

get the current volume:

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

mute:

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

unmute:

player.getAudioComponent().setVolume(currentVolume);
Hadi
  • 125
  • 1
  • 4
4

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);
Asuk Nath
  • 570
  • 4
  • 10
2

try

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

analogous to this line of code

thomasb
  • 95
  • 8
  • which is exactly the same as `player.selectTrack(FullPlayer.TYPE_AUDIO, -1);` with `ExoPlayer.TRACK_DEFAULT`. This is totaly not a good solution as unmuting the sound will have major delay – Hugo Gresse Jun 17 '16 at 08:25
  • exactly the same but different. Anyway there are situations in which you want to completely disable the audio track (e.g. testing purpose) – thomasb Sep 28 '16 at 12:39
  • it's exactly my second case in the accepted answer 'The other one" – Hugo Gresse Sep 28 '16 at 14:54
2

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

Allen Vork
  • 1,536
  • 3
  • 16
  • 29
0

Mute ExoPlayer using Kotlin

player.audioComponent?.volume = 0f
Cube
  • 362
  • 4
  • 8
0

Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).

You should use new version: implementation 'com.google.android.exoplayer:exoplayer:2.18.5'

  • Mute volume:
    exoPlayer.volume = 0f    
  • Unmute volume value:
    exoPlayer.volume = 1f   
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32