简体   繁体   中英

How to play streaming video at Android by using VideoView/MediaPlayer

I have a web server which supports streaming video. So it correctly handles range HTTP header.

Is there a way load video chunk by chunk by using VideoView/MediaPlayer? Currently I just set video URL by using VideoView.setVideoURI method and start playing video at onPrepared handler. As I can see onPrepared handler is invoked after the whole video file is loaded.

You can try something like this :

try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
//            Log.e("Error", e.getMessage());
            pDialog.dismiss();
            e.printStackTrace();

        } finally {
            videoview.setOnErrorListener(new OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    CommonUtilities.showToast(VideoViewActivity.this, "Video Format not supported by device.");
                    VideoViewActivity.this.finish();
                    return true;
                }
            });
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });

Please try this code it works with buffering and plays continuously with mediacontroller.

     try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM