我对android MediaPlayer的getduration()方法有以下问题。例如,使用以下文件(文件的实际持续时间为5747000毫秒):
准备好MediaPlayer后,我将在log cat当前位置和getduration值中打印以下代码:
Log.d("Podcast", "getDuration:"+mediaPlayer.getDuration());
Log.d("Podcast","getCurrentPosition:"+mediaPlayer.getCurrentPosition());
在log cat中我可以看到以下内容:
D/Podcast﹕ getDuration:2885642
D/Podcast﹕ getCurrentPosition:3288059
我发现另一篇文章有关于此的信息,但我还没有找到解决方案。 Similar issue found on stackoverflow
怎么可能?任何人都可以帮我吗?
感谢。问候。
答案 0 :(得分:0)
根据这个帖子:https://code.google.com/p/android/issues/detail?id=2559 mp3文件应该是44,100赫兹。否则,MediaPlayer将按MP3的实际采样率与44,100 Hz的比例缩放时间。
答案 1 :(得分:0)
你必须把它转换成毫秒..
public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
String mp3Minutes = "";
// Convert total duration into time
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
// menor que 10
if (minutes < 10) {
mp3Minutes = "0" + minutes;
} else {
mp3Minutes = "" + minutes;
}
finalTimerString = finalTimerString + mp3Minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}
然后,
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if (mediaPlayer != null && isPlaying) {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
musicSongDurationStart.setText("" + utils.milliSecondsToTimer(currentDuration) + "/" + utils.milliSecondsToTimer(totalDuration));
}
if (!isPause) {
musicSongDurationStart.postDelayed(mUpdateTimeTask, 100);
}
// Running this thread after 100 milliseconds
}
};
我希望它会对你有所帮助。