Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...