Change the volume of a specif player and get its c

2019-07-23 05:13发布

问题:

I've this class (my media player class) and I need to create 2 methods on it, one to change the volume and other to get the current volume. (I'll create a seekbar to change the player volume), ok.

public class LoopMediaPlayer {

    private Context ctx = null;
    private int rawId = 0;

    private MediaPlayer currentPlayer = null;
    private MediaPlayer nextPlayer = null;

    public static LoopMediaPlayer create(Context ctx, int rawId) {
        return new LoopMediaPlayer(ctx, rawId);
    }

    private LoopMediaPlayer(Context ctx, int rawId) {
        this.ctx = ctx;
        this.rawId = rawId;

        currentPlayer = MediaPlayer.create(this.ctx, this.rawId);
        currentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                currentPlayer.start();
            }
        });

        createNextMediaPlayer();
    }

    private void createNextMediaPlayer() {
        nextPlayer = MediaPlayer.create(ctx, rawId);
        currentPlayer.setNextMediaPlayer(nextPlayer);
        currentPlayer.setOnCompletionListener(onCompletionListener);
    }

    private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.release();
            currentPlayer = nextPlayer;

            createNextMediaPlayer();
        }
    };

    public void stopPlayer() {
        if (currentPlayer != null && currentPlayer.isPlaying()) {
            currentPlayer.stop();
            currentPlayer.release();
        }

        if (nextPlayer != null && nextPlayer.isPlaying()) {
            nextPlayer.stop();
            nextPlayer.release();
        }
    }


    public void setVolume(float vol) {
        //todo
        currentPlayer.setVolume(vol, vol);//don't work
    }


    public int getVolume(){
        //todo
        return 0;
    }

}

and my seekBar progress and listener

seekBar.setProgress(player.getVolume());//use the method that retrieve the current volume

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, final int i, boolean b) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        player.setVolume(i);//use the method that change the volume
                    }
                });
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

I tried mp.setVolume() but didn't change any thing. And I can't get the current volume to set like the seekbar progress before show it.

Details - All Audios are in raw file (not stream). There will be more than one player playing at same time, them will be stored so I can retrieve any of them any time, and I want to change and retrieve just the volume of a specific player not of all them at same time.

Anyone know how I should implement both this methods, 'cause I have no idea.

Thanks