VideoView对HLS来说太早触发OnPreparedListener

时间:2012-12-12 01:41:03

标签: android http-live-streaming

我希望在等待VideoView开始播放HLS时向用户显示一些ProgressDialog。我尝试使用OnPreparedListener,但他提前触发(播放器下载m3u8文件后,而不是视频启动时)。

VideoView player = (VideoView) findViewById(R.id.player);
String httpLiveUrl = "http://example.com/playlist.m3u8";

player.setVideoURI(Uri.parse(httpLiveUrl));
player.setMediaController(new MediaController(this));
player.requestFocus();

player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
    progress.dismiss();
}
});

progress.show();
player.start();

我没有找到另一个合适的排行榜......可能有人知道sloution吗?

2 个答案:

答案 0 :(得分:15)

感谢所有人。我用下一个黑客解决了我的问题:

videoView.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {

        final Handler handler = new Handler();
        videoPositionThread = new Runnable() {
             public void run() {
                try {
                    int duration = videoView.getCurrentPosition();
                    if (!(videoPosition == duration && videoView.isPlaying())) {
                        progress.dismiss();
                    }
                    videoPosition = duration;
                    handler.postDelayed(videoPositionThread, 1000);
                } catch (IllegalArgumentException e) {
                    Log.d(TAG, e.getMessage(), e);
                }
            }
        };
        handler.postDelayed(videoPositionThread, 0);
    }
});

答案 1 :(得分:1)

我遇到了同样的问题,只是找到了满足我需求的解决方案。也许这也适合你。至少它经过了测试和测试。适用于Android 2.2,2.3和4.2。

我们的想法是定期检查videoView当前位置是否大于零。这是mikhail答案的修改。感谢mikhail:)

public class VideoViewActivity extends Activity {

    // Declare variables
    ProgressDialog pDialog;
    VideoView videoview;
    Runnable videoPositionThread;

    // Insert your Video URL
    String VideoURL = "enter your video rtsp url here";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview_main);
        videoview = (VideoView) findViewById(R.id.VideoView);

        pDialog = new ProgressDialog(VideoViewActivity.this);
        pDialog.setTitle("Android Video Streaming Tutorial");
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

        Uri video = Uri.parse(VideoURL);
        videoview.setVideoURI(video);
        videoview.start();

        final Handler handler = new Handler();
        videoPositionThread = new Runnable() {
            public void run() {
                int currentPosition = videoview.getCurrentPosition();

                if (currentPosition > 0)
                    pDialog.dismiss();
                else
                    handler.postDelayed(videoPositionThread, 1000);
            }
        };

        handler.postDelayed(videoPositionThread, 0);
    }
}