在ExoPlayer上静音 [英] Mute audio on ExoPlayer
问题描述
我正在使用名为ExoPlayer的Google新MediaPlayer,却找不到静音的方法
I'm using Google new MediaPlayer named ExoPlayer and cannot find a way to mute the sound
是否有一种简单的方法可以使Google ExoPlayer上的音轨静音?或更改音量?
Is there an easy way to mute audio track on Google ExoPlayer ? Or changing volume ?
推荐答案
Exoplayer 2.x.x
获取当前音量:
int currentvolume = player.getVolume();
静音: player.setVolume(0f);
取消静音: player.setVolume(currentVolume);
Exoplayer 2.x.x
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.
基本上,您需要获取作为ExoPlayerComponent
的audioTrackRenderer
并向其发送消息.
所以:
Basicly, you need to get the audioTrackRenderer
which is a ExoPlayerComponent
and send message to it.
So :
-
添加
audioRenderer
成员并将其设置在onRenderers
中:
Add
audioRenderer
member and set it inonRenderers
:
// 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);
}
}
用法:
静音:player.setMute(true);
取消静音:player.setMute(false);
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);
这篇关于在ExoPlayer上静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!