5

我有一个 mp3 文件,我的应用程序必须寻找该 mp3 文件的某个选定时间,然后从那里开始播放。

我通过这种方法将我的字符串时间转换为 int 值

private static int convert(String time) {
    int quoteInd = time.indexOf(":");
    int pointInd = time.indexOf(".");

    int min = Integer.valueOf(time.substring(0, quoteInd));
    int sec = Integer.valueOf(time.substring(++quoteInd, pointInd));
    int mil = Integer.valueOf(time.substring(++pointInd, time.length()));

    return (((min * 60) + sec) * 1000) + mil;
} 

注意:我的刺痛是这样的,这5:12.201意味着 5 分 12 秒和 201 毫秒。

我从MP3 Audio Editor应用程序(适用于 Windows)中获得了这些时间,并使用应用程序检查了主题KMPlayer(适用于 Windows)。这些时间对他们俩都是正确的。

但是在我的应用程序中,当我寻找MediaPlayer到那个时间时,音频不会从我选择的位置开始。 (时间正确,但声音不同。)


我认为那MediaPlayer不正确地寻求那个时间。所以我getCurrentPosition()在玩之前通过调用来检查当前位置,但返回值和搜索值是相同的。

我对此一无所知。


编辑:

我的问题不是时间转换。

我正确地转换并寻找到那里,但它播放了当时没有预料到的东西。

这意味着 KMPlayer 和 Android 中的时间是不同的。

我的问题是方式?又该如何解决呢?

4

2 回答 2

3

你必须寻求使用seekTo方法。这需要毫秒作为时间偏移量。您的偏移量是您想要播放的距离开始多远,例如距离开始 1 分钟可以是您的偏移量。

注意:我的字符串是这样的 5:12.201 (Mins : Secs : Millisecs)

如果你想寻找一个像5:12.201那么使用的时间seekTo(312201);

解释

1000毫秒给你一秒,所以12000是 12 秒,而60000是一分钟。

如果您需要5m:12s时间,请执行以下操作:

MyMins = 1000 * 60 * 5; //# 5 mins at 60 secs per minute
MySecs = 1000 * 12; //# 12 secs
MyMilliSecs = 201; //# 201 millisecs
SeekValue = (MyMins + MySecs + MyMilliSecs);
seekTo(SeekValue); //# seeks to 312201 millisecs (is == 5 min & 12 secs & 201 ms)
于 2016-05-16T06:27:51.537 回答
1

我通常使用这个功能。

我认为它可以帮助你。

 public static String milliSecondsToTimer(long milliseconds){
    String finalTimerString = "";
    String secondsString = "";

    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);
    if(hours > 0){
        finalTimerString = hours + ":";
    }

    if(seconds < 10){
        secondsString = "0" + seconds;
    }else{
        secondsString = "" + seconds;}

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

    return finalTimerString;
}

祝你好运 :)

于 2016-05-17T12:09:46.873 回答