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 ?
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);
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: developer.android.com/guide/topics/media-apps/… –
Cherida 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);
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
}
In ExoPlayer 2.x.x:
get the current volume:
int currentVolume = player..getAudioComponent().getVolume();
mute:
player.getAudioComponent().setVolume(0f);
unmute:
player.getAudioComponent().setVolume(currentVolume);
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);
try
player.setSelectedTrack(DemoPlayer.TYPE_AUDIO, DemoPlayer.TRACK_DISABLED);
analogous to this line of code
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 –
Cherida Just simply use player.setVolume(0) will silent the video.
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'
exoPlayer.volume = 0f
exoPlayer.volume = 1f
Mute ExoPlayer using Kotlin
player.audioComponent?.volume = 0f
© 2022 - 2024 — McMap. All rights reserved.
player.volume = 0f
– Hoenack