gpt4 book ai didi

java - 媒体播放器搜索栏搜索无法正常工作

转载 作者:行者123 更新时间:2023-12-02 01:53:46 24 4
gpt4 key购买 nike

目前我正在尝试修复我的搜索栏上的一个错误,当我单击确定的位置时,搜索栏和媒体播放器重置为 0。请检查下面的代码:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
final Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (mediaPlayer != null) {
int CurrentPosition = mediaPlayer.getCurrentPosition();
int Duration = mediaPlayer.getDuration();

int progress = (getProgressPercentage(CurrentPosition, Duration));
seekBar.setProgress(progress); // this line is working perfectly, I'm getting a high accuracy using getProgressPercentage

String m_1;
String s_1;
//Toast.makeText(getApplicationContext(), "Current: " + CurrentPosition + " - Duration: " + mediaPlayer.getDuration(), Toast.LENGTH_LONG).show();

final int minutes_1 = (CurrentPosition / 1000) / 60;
final int seconds_1 = ((CurrentPosition / 1000) % 60);

if (minutes_1 < 10) {
m_1 = "0" + minutes_1;
} else {
m_1 = "" + minutes_1;
}

if (seconds_1 < 10) {
s_1 = "0" + seconds_1;
} else {
s_1 = "" + seconds_1;
}
time1.setText(m_1 + ":" + s_1);


String m_2;
String s_2;
final int minutes_2 = (Duration / 1000) / 60;
final int seconds_2 = ((Duration / 1000) % 60);

if (minutes_2 < 10) {
m_2 = "0" + minutes_2;
} else {
m_2 = "" + minutes_2;
}

if (seconds_2 < 10) {
s_2 = "0" + seconds_2;
} else {
s_2 = "" + seconds_2;
}

time2.setText(m_2 + ":" + s_2);

}
mHandler.postDelayed(this, 1000);
}
};
mRunnable.run();

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {

int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));
mediaPlayer.seekTo(_progress); //the problem is here, the seetkTo is reseting to zero.
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});


}


public int getProgressPercentage(long currentDuration, long totalDuration){
Double percentage = (double) 0;

long currentSeconds = (int) (currentDuration / 100);
long totalSeconds = (int) (totalDuration / 100);

percentage =(((double)currentSeconds)/totalSeconds)*100;

return percentage.intValue();
}

此脚本中没有错误,一切正常,除了当我单击确定的位置时,我正在使用 int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));getProgressPercentage我在 final Runnable mRunnable = new Runnable() { 工作得很好。我使用此函数获得了更高的准确性。

所以,这个独特的问题与 seekBar.setOnSeekBarChangeListener 有关。 。我不知道如何解决这个问题,你能帮我吗?谢谢。

最佳答案

int _progress = (getProgressPercentage(progress*1000, mediaPlayer.getDuration()));
mediaPlayer.seekTo(_progress); //the problem is here, the seetkTo is reseting to zero.
在您的情况下,

_progress 是已播放的媒体的百分比,而不是绝对持续时间(以毫秒为单位)mediaPlayer.seekTo(long millis) 预计时间以 millis 为单位。相反,您正在通过百分比。像这样更改它:

 int _progress = (progress * mediaPlayer.getDuration())/100;
mediaPlayer.seekTo(_progress); // PROBLEM SOLVED

关于java - 媒体播放器搜索栏搜索无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598218/

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