gpt4 book ai didi

java - (Android)如何在我的音乐播放器上显示时间

转载 作者:太空狗 更新时间:2023-10-29 15:38:03 25 4
gpt4 key购买 nike

音乐可以正常播放,但我的音乐播放器无法显示音乐时间,我该怎么办?

在主类中

 public void onClick(View v) {

if (mMedia.isPlaying()) {
txtView.setText("Playing : music.mp3....");
mMedia.pause();
} else {
txtView.setText("pause : music.mp3....");
mMedia.start();
}
}
private void UpdateseekChange(View v){
if(mMedia.isPlaying()){
SeekBar sb = (SeekBar)v;
mMedia.seekTo(sb.getProgress());
}

最佳答案

这个应该可以工作:

public void onClick(View v) {

if (mMedia.isPlaying()) {
txtView.setText("Playing : music.mp3....");
mMedia.pause();
} else {
txtView.setText("pause : music.mp3....");
mMedia.start();
txtView.post(mUpdateTime);
}
}

private Runnable mUpdateTime = new Runnable() {
public void run() {
int currentDuration;
if (mMedia.isPlaying()) {
currentDuration = mp.getCurrentPosition();
updatePlayer(currentDuration);
txtView.postDelayed(this, 1000);
}else {
txtView.removeCallbacks(this);
}
}
};

private void updatePlayer(int currentDuration){
txtView.setText("" + milliSecondsToTimer((long) currentDuration));
}

/**
* Function to convert milliseconds time to Timer Format
* Hours:Minutes:Seconds
* */
public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";

// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}

// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}

finalTimerString = finalTimerString + minutes + ":" + secondsString;

// return timer string
return finalTimerString;
}

当轨道完成时,您当然需要删除 runnable。

关于java - (Android)如何在我的音乐播放器上显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com