VideoView triggers OnPreparedListener too early for HLS

后端 未结 2 912
别那么骄傲
别那么骄傲 2021-02-20 11:09

I want to show user some ProgressDialog while he waits for VideoView to start play HLS. I try to use OnPreparedListener for this, but he triggers to early (after player download

相关标签:
2条回答
  • 2021-02-20 11:45

    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);
        }
    });
    
    0 讨论(0)
  • 2021-02-20 11:54

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题