9

给出了以下源代码片段:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {

            mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END){
                        activity.dismissDialog(DialogID.DIALOG_LOADING);
                        return true;
                    } 
                    return false;
                }
            });
        }
    });

我正在使用 Android 3.x+ 设备流式传输 HLS 流,并在缓冲完成后尝试隐藏加载对话框。视频流有效,但永远不会触发信息事件。

有任何想法吗?

4

7 回答 7

3

我知道为时已晚,但是将其发布给仍在寻求解决方案的用户(这对我有用):

        progressDialog.show();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END){
                            progressDialog.dismiss();
                            return true;
                        } else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START){
                            progressDialog.show();
                        }
                        return false;
                    }
                });
                progressDialog.dismiss();
                videoView.start();
            }
        });
于 2016-12-27T13:27:41.540 回答
2

你是对的,事件永远不会被触发。这是一个已知的 HLS 错误,我认为 Google 不会修复。

这适用于 onInfo 和缓冲事件。

请参阅https://code.google.com/p/android/issues/detail?id=42767https://code.google.com/p/googletv-issues/issues/detail?id=2

对不起!

于 2013-08-07T04:39:55.000 回答
2

Not fully sure as to what the OP is asking, but here are some very untimely bits of information.

I wouldn't rely on onPrepared. I find it to be unreliable.

I have found the two most useful pieces of information for HLS streaming through the MediaPlayer are the duration of the video and the progress position of the video. You get both of these by listening to progress updates.

When the duration is greater than zero, you know the video is truly prepared and can be manipulate (scrub). When progress position changes, you know the video is done buffering and has commenced playback. This last item only works when the video is playing of course. The MediaPlayer tends to relay inaccurate information.

These pieces of information are mostly accurate and can usually be relied upon to be "fairly" timely. This timeliness varies from device to device.

于 2013-10-23T03:25:12.890 回答
1

onPreparedMediaPlayer在准备开始缓冲时调用,而不是在视频完全缓冲时调用。但是,从onPrepared方法中关闭加载对话框是完全自然的。

在填充缓冲区后恢复播放MEDIA_INFO_BUFFERING_END使用它,所以我认为它不应该用来关闭对话框。所以这应该工作:MediaPlayer

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        activity.dismissDialog(DialogID.DIALOG_LOADING);
    }
});
于 2012-11-07T21:32:04.050 回答
0

如果您想在每次缓冲时显示加载(初始时间或后续缓冲区欠载),只需确保再次显示它:

// at the beginning
show

boolean onInfo(int what, int extra) {
  switch (what) {
    case MEDIA_INFO_BUFFERING_END:
      "hide";
      break;
    case MEDIA_INFO_BUFFERING_START
      "show":
  }
}

因此,此事件序列将按需要执行:

- whenever you start (setVideoURI or start): show
- onPrepared: just plug the info listener
- onInfo BUFFERING_END   hide (it's playing)
- onInfo BUFFERING_START show (it's buffering again)
- onInfo BUFFERING_END   hide (it's playing)

更新:

这是假设信息事件有效。当然。

于 2014-07-02T14:50:27.160 回答
0

您无需通过 setOnInfoListener

通过覆盖 setOnPreparedListener 方法就足够了。就像在 api 节目中一样

公共无效 setOnPreparedListener (MediaPlayer.OnPreparedListener l)

注册要在媒体文件加载并准备就绪时调用的回调。

所以,你可以在 setOnPreparedListener 方法中关闭你的对话框就足够了

像这样

    vv.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {

            handler.post(new Runnable() {
            @Override
        public void run() {
                Toast.makeText(MainActivity.this, "finish11", Toast.LENGTH_LONG).show();
        }
        });
        }
    });
于 2012-11-06T09:10:59.323 回答
0

您可以在 videoView 上设置 OnPreparedListener 因为它是您的对象,但是如果您检查VideoView的源,您会发现 mMediaPlayer 是它的私有成员,因此您从外部所做的任何更改都不会应用于它。

根据您的要求,您需要缓冲状态,以便您可以拥有线程或处理程序或某些东西,以便您可以更新 UI 以获取缓冲状态,有一种方法

int percent = videoView.getBufferPercentage();

if(percent == 100){
// buffering done 
} 
于 2012-11-01T12:23:29.210 回答