在ExoPlayer上静音 [英] Mute audio on ExoPlayer

查看:808
本文介绍了在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.

基本上,您需要获取作为ExoPlayerComponentaudioTrackRenderer并向其发送消息. 所以:

Basicly, you need to get the audioTrackRenderer which is a ExoPlayerComponent and send message to it. So :

  1. 添加audioRenderer成员并将其设置在onRenderers中:

  1. 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);
        }
    }
    

  • 用法:
    静音: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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆