我有一个 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;
}
注意:我的 stings 是这样的 5:12.201
,表示 5 分 12 秒 201 毫秒。
我从 MP3 Audio Editor
应用程序(适用于 windows)获取这些时间,并使用 KMPlayer
应用程序(适用于 windows)检查主题。这些时间对他们两人来说都是正确的。
但是在我的应用程序中,当我到那个时候寻找我的 MediaPlayer
时,音频不是从我选择的位置开始的。(时间是正确的,但声音是不同的。)
我认为 MediaPlayer
没有正确定位到那个时间。所以我在播放之前通过调用 getCurrentPosition()
检查了当前位置,但返回值和搜索值相同。
我对此一无所知。
编辑:
我的问题不是时间转换。
我正确地转换并寻找到那里,但它播放了当时没有预料到的东西。
这意味着 KMPlayer 和 Android 中的时间是不同的。
我的问题是Way? 和How is solve it?
最佳答案
您必须使用 seekTo
进行搜索 方法。这需要 毫秒 作为时间偏移量。您的偏移量是您想要播放的距离开始的距离,例如从开始开始 1 分钟可以作为您的偏移量。
Note: my strings is like this 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)
关于Android SDK MediaPlayer 没有正确寻找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089289/