如何加快歌曲播放速度

时间:2019-04-25 07:35:17

标签: android

我刚刚开发了一个简单的媒体播放器,可以播放一首歌曲。现在,我想添加一个按钮,以加快歌曲播放速度,该速度是正常播放速度的两倍。
 如果可能的话,我也想知道如何减慢歌曲的播放速度。

我该怎么做?
 在我的代码中,我已经完成了OnClick方法,它称为“ speedBtnClick”。

这是我的代码:

public class MainActivity extends AppCompatActivity {

Button playBtn;
Button speedBtn;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    playBtn = (Button) findViewById(R.id.playBtn);
    speedBtn = (Button) findViewById(R.id.speedBtn);
    elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
    remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel);

    // Media Player
    mp = MediaPlayer.create(this, R.raw.song);
    mp.setLooping(true);
    mp.seekTo(0);
    mp.setVolume(0.5f, 0.5f);
    totalTime = mp.getDuration();

    // Position Bar
    positionBar = (SeekBar) findViewById(R.id.positionBar);
    positionBar.setMax(totalTime);
    positionBar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser) {
                        mp.seekTo(progress);
                        positionBar.setProgress(progress);
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            }
    );


    // Volume Bar
    volumeBar = (SeekBar) findViewById(R.id.volumeBar);
    volumeBar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    float volumeNum = progress / 100f;
                    mp.setVolume(volumeNum, volumeNum);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            }
    );

    // Thread (Update positionBar & timeLabel)
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (mp != null) {
                try {
                    Message msg = new Message();
                    msg.what = mp.getCurrentPosition();
                    handler.sendMessage(msg);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
            }
        }
    }).start();

}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int currentPosition = msg.what;
        // Update positionBar.
        positionBar.setProgress(currentPosition);

        // Update Labels.
        String elapsedTime = createTimeLabel(currentPosition);
        elapsedTimeLabel.setText(elapsedTime);

        String remainingTime = createTimeLabel(totalTime-currentPosition);
        remainingTimeLabel.setText("- " + remainingTime);
    }
};

public String createTimeLabel(int time) {
    String timeLabel = "";
    int min = time / 1000 / 60;
    int sec = time / 1000 % 60;

    timeLabel = min + ":";
    if (sec < 10) timeLabel += "0";
    timeLabel += sec;

    return timeLabel;
}

public void playBtnClick(View view) {

    if (!mp.isPlaying()) {
        // Stopping
        mp.start();
        playBtn.setBackgroundResource(R.drawable.ic_stop);

    } else {
        // Playing
        mp.pause();
        playBtn.setBackgroundResource(R.drawable.ic_play);
    }

}

public void speedBtnClick(View view){

}                                                                        
}

1 个答案:

答案 0 :(得分:3)

您可以使用PlaybackParams对象执行此操作。

PlaybackParams pp = new PlaybackParams();

//speed *2
pp.setSpeed(2f);
//speed /2
pp.setSpeed(0.5f);

mp.setPlaybackParams(pp);

警告

您需要Api 23来做到这一点(Android 6.0)