更改特定播放器的音量并获取其当前音量

时间:2016-10-29 18:49:04

标签: android audio media-player

我是这个类(我的媒体播放器类),我需要在其上创建2个方法,一个用于更改音量,另一个用于获取当前音量。 (我将创建一个搜索栏以更改播放器音量),好的。

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;
    }

}

和我的seekBar进度和监听器

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) {

            }
        });

我尝试过mp.setVolume()但没有改变任何事情。在显示之前,我无法将当前音量设置为像搜索栏进度一样。

详细信息 - 所有Audios都在原始文件中(不是流)。将会有多个播放器同时播放,它们将被存储,以便我可以随时检索其中的任何一个,并且我想 更改并检索其中的音量一个特定的玩家并非所有玩家同时

任何人都知道我应该如何实施这两种方法,因为我不知道。

由于

0 个答案:

没有答案