VideoView triggers OnPreparedListener too early for HLS

只愿长相守 提交于 2019-12-04 02:59:38

Thanks to all. I solved my problem with next hack:

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);
    }
});
herdiansyah.ep

I experienced the same issue, and just found a solution for my needs. Maybe this works for you, too. At least it's tested & works on Android 2.2, 2.3, and 4.2.

The idea is to periodically check if the videoView current position is greater than zero. It's a modification of mikhail's answer. Thanks to mikhail, too :)

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!