How to design custom seekbar and volume control for ExoPlayer in Android

為{幸葍}努か 提交于 2019-12-06 05:16:05

AudioManager is helpful for handling the system volume. I have implemented horizontal volume Seekbar to control audio volume with ExoPlayer. Just use this :

Create AudioManager object :

    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

Handle Volume up/down Hardware Button :

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN){
            volumeSeekBar.setProgress(audioManager.getStreamVolume(exoPlayer.getAudioStreamType()));
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP){
            volumeSeekBar.setProgress(audioManager.getStreamVolume(exoPlayer.getAudioStreamType()));
        }
        return super.onKeyUp(keyCode, event);
    }

Handle volume using Seekbar :

volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                audioManager.setStreamVolume(exoPlayer.getAudioStreamType(), i, 0);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

I've created a solution which works (at least for me, anyway) and creates a vertical SeekBar. Create Java class like this Hope this will help you.

If I read the ExoPlayer source code correctly you have to keep references to the audioRenderers you use when preparing the ExoPlayer instance.

exoPlayer.prepare(audioRenderer);

To change volume you have to send the following message:

exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 0.1f);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!