ProgressPar在MediaPlayer播放时被阻止
我正在尝试制作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();
}
}
在调试时, ProgressBar
或CircularProgressView
.setProgress()
在音频再现过程中被调用,但它仍然indeterminate
被阻塞,但progress_view.setIndeterminate(false)
也被调用,并且它不会变得不确定。
当音频结束时, ProgressBar
或CircularProgressView
会获得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]);
}
}
并且输出总是:
不确定状态:是
*为什么? *:'(
可能你想调用mediaFileLengthInMilliseconds = mp.getDuration();
在调用publishProgress((int)-1);
而不是之后,或根本不使用该成员,因为您已将MediaPlayer
作为成员保存,您可以直接查询它。
编辑
要解决视频中的问题,您必须使用以下样式style="@android:style/Widget.ProgressBar.Horizontal
。通过循环ProgressBar
, setIndetermiate
标志将被忽略