gpt4 book ai didi

android - MediaPlayer 播放时 ProgressBar 被阻塞

转载 作者:行者123 更新时间:2023-11-30 01:52:29 24 4
gpt4 key购买 nike

我正在尝试使 MediaPlayer 循环播放,因此它在播放时准备和设置进度时不确定

我尝试了很多方法,但这是我的最后一个:我在 AsyncTask<Object, Object, Object> 上更新栏.

我使用的类是 CircularProgressView 但我试着换成ProgressBar并且行为是相同:

public void startPlaying(final Audio audio, final CircularProgressView progress_view) {

if (!isPLAYING) {
progress_view.setIndeterminate(true);
progress_view.setVisibility(View.VISIBLE);
progress_view.startAnimation();
isPLAYING = true;
audioPlaying = audio;
mp = new MediaPlayer();

new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... objects) {
try {
mp.prepare();
publishProgress((int)-1);

mediaFileLengthInMilliseconds = mp.getDuration();
mp.start();

while (mp != null && mp.isPlaying()) {
Thread.sleep(100);
publishProgress((int) (mp.getCurrentPosition()));
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
try {
mp.setDataSource(audio.getFile().getUrl());
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPlaying();
publishProgress((int)-2);
}
});
}

@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
int value = (int)values[0];
if (value == -1) {
progress_view.setIndeterminate(false);
progress_view.setMaxProgress(mediaFileLengthInMilliseconds);
} else if (value == -2) {
progress_view.setVisibility(View.GONE);
} else {
progress_view.setProgress((int) values[0]);
System.out.println("setting: " + values[0]);
}
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}.execute();

} else {
isPLAYING = false;
stopPlaying();
}
}

调试时,ProgressBarCircularProgressView .setProgress()在音频再现期间被调用,但它在 indeterminate 中仍然被阻止,但是progress_view.setIndeterminate(false)也被调用,并且它不会变得不确定。

当音频结束时,ProgressBarCircularProgressView获得 100% 的进度。

有什么线索吗?

非常感谢您。


编辑:

正如@Blackbelt 所建议的,我不得不调用 mediaFileLengthInMilliseconds = mp.getDuration();之前publishProgress((int)-1);

但现在这是行为:

https://drive.google.com/file/d/0B-V0KHNRjbE_b3hkbkwtTXhIeTg/view?usp=sharing


编辑 2:mp.getDuration() 的日志和 mp.getCurrentPosition() :

http://pastebin.com/g93V5X9F

或者这可能更清楚:

http://pastebin.com/kYm01Gpg

调用这部分:

            @Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
int value = (int)values[0];
if (value == -1) {
progress_view.setIndeterminate(false);
progress_view.setMaxProgress(mediaFileLengthInMilliseconds);
Log.i("Stackoverflow:", "setMaxProgress to " + mediaFileLengthInMilliseconds);
} else if (value == -2) {
progress_view.setVisibility(View.GONE);
} else {
progress_view.setProgress((int) values[0]);
Log.i("Stackoverflow:", "setProgress to " + (int)values[0]);

}
}

编辑 3:我正在添加新代码,注释掉函数 setIndeterminate(boolean)并换成 ProgressBar :

XML:

<ProgressBar
android:id="@+id/progress_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:progress="40"
android:visibility="gone"
android:layout_alignTop="@+id/play_button"
android:layout_alignLeft="@+id/play_button"
android:layout_alignStart="@+id/play_button"
android:layout_alignRight="@+id/play_button"
android:layout_alignEnd="@+id/play_button"
android:layout_alignBottom="@+id/play_button" />

Java:

public void startPlaying(final Audio audio, final ProgressBar progress_view) {

if (!isPLAYING) {
Log.i("Stackoverflow", "startPlaying called");
audio.put("times_listened", audio.getTimes_Listened() + 1);
audio.saveEventually();

progress_view.setVisibility(View.VISIBLE);
isPLAYING = true;
audioPlaying = audio;
mp = new MediaPlayer();

new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... objects) {
try {
mp.prepare();
mediaFileLengthInMilliseconds = mp.getDuration();
publishProgress((int)-1);


mp.start();

while (mp != null && mp.isPlaying()) {
Thread.sleep(100);
publishProgress((int) (mp.getCurrentPosition()));
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
try {
mp.setDataSource(audio.getFile().getUrl());
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPlaying();
publishProgress((int)-2);
}
});
}

@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
int value = (int)values[0];
if (value == -1) {
progress_view.setMax(mediaFileLengthInMilliseconds);
Log.i("Stackoverflow:", "setMaxProgress to " + mediaFileLengthInMilliseconds);
} else if (value == -2) {
progress_view.setVisibility(View.GONE);
} else {
progress_view.setProgress((int) values[0]);
Log.i("Stackoverflow:", "setProgress to " + (int)values[0]);

System.out.println("setting: " + values[0]);
}
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}.execute();

} else {
isPLAYING = false;
stopPlaying();
}
}

编辑 4:

我调用 progress_view.isDeterminate()它正在返回我true每次。所以我决定在修改进度的时候做:

 @Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
int value = (int)values[0];
if (value == -1) {
progress_view.setMax(mediaFileLengthInMilliseconds);
Log.i("Stackoverflow:", "setMaxProgress to " + mediaFileLengthInMilliseconds);
} else if (value == -2) {
progress_view.setVisibility(View.GONE);
} else {
if (progress_view.isIndeterminate()) // LOOK AT THIS
progress_view.setIndeterminate(false);
progress_view.setProgress((int) values[0]);
Log.i("Stackoverflow:", "setProgress to " + (int) values[0]);
Log.i("Stackoverflow", "Indeterminate state: " + progress_view.isIndeterminate());

System.out.println("setting: " + values[0]);
}
}

输出总是:

Indeterminate state: true

*为什么? * :'(

最佳答案

可能您想在调用 publishProgress((int)-1); 之前调用 mediaFileLengthInMilliseconds = mp.getDuration(); 而不是之后,或者不使用该成员总之,因为您已经将 MediaPlayer 保留为成员,所以您可以直接查询它。

编辑

要解决视频中的问题,您必须使用以下样式 style="@android:style/Widget.ProgressBar.Horizo​​ntal。使用圆形 ProgressBar , setIndetermiate 标志被忽略

关于android - MediaPlayer 播放时 ProgressBar 被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800312/

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