MediaPlayer 播放时 ProgressBar 被阻塞

ProgressBar blocked while MediaPlayer is playing

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

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

上更新栏

我正在使用的 class 是 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 所建议的,我必须在 publishProgress((int)-1);

之前调用 mediaFileLengthInMilliseconds = mp.getDuration();

但现在是这样的行为:

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


编辑 2mp.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("Whosebug:", "setMaxProgress to " + mediaFileLengthInMilliseconds);
                } else if (value == -2) {
                    progress_view.setVisibility(View.GONE);
                } else {
                    progress_view.setProgress((int) values[0]);
                    Log.i("Whosebug:", "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("Whosebug", "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("Whosebug:", "setMaxProgress to " + mediaFileLengthInMilliseconds);
                } else if (value == -2) {
                    progress_view.setVisibility(View.GONE);
                } else {
                    progress_view.setProgress((int) values[0]);
                    Log.i("Whosebug:", "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("Whosebug:", "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("Whosebug:", "setProgress to " + (int) values[0]);
                    Log.i("Whosebug", "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.Horizontal。使用循环 ProgressBar,忽略 setIndetermiate 标志