Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mute audio on ExoPlayer

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 ?

like image 245
Hugo Gresse Avatar asked Dec 10 '14 15:12

Hugo Gresse


People also ask

What is the use of ExoPlayer?

ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV. ExoPlayer is highly customizable and extensible, making it capable of many advanced use cases.

Does ExoPlayer use MediaPlayer?

ExoPlayer is a media player library that provides a way to play audio and video with lots of customization in it. It is an alternative that is used to play videos and audios in Android along with MediaPlayer. ExoPlayer is a library that is the best alternative source for playing audio and videos on Android.

How do I create an ExoPlayer instance?

So this is how you initialize simpleExoPlayer: Activity activity = getActivity(); // if you are in a fragment // Or, activity = YourActivity. this; if you are in an Activity SimpleExoPlayer simpleExoPlayer = new SimpleExoPlayer. Builder(activity).


3 Answers

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);
like image 187
Hugo Gresse Avatar answered Oct 18 '22 20:10

Hugo Gresse


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);
like image 25
Khoosham Avatar answered Oct 18 '22 20:10

Khoosham


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
}
like image 8
Ashav Kothari Avatar answered Oct 18 '22 21:10

Ashav Kothari