Streaming videos on VideoView

ⅰ亾dé卋堺 提交于 2020-01-25 05:45:11

问题


I have a simple VideoView and I'm trying to stream a video from a URL. Here's my code:

//everything is in onCreate()

videoView = (VideoView)findViewById(R.id.video);

String videoAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
Uri videoUri = Uri.parse(videoAddress);

videoView.setVideoURI(videoUri);
videoView.start();
videoController = new MediaController(this);
videoController.setAnchorView(videoView);
videoView.setMediaController(videoController);

The Problem:

It takes close to 7-8 seconds to start the video. Keeps buffering till then.

It isn't about the internet connectivity because I played the same video, in the same internet and at the same time, in the browser. (running the browser parallel to the app). And it took only 1-2 seconds to start the video in the browser.

I tried several other videos too from different sources, and I'm facing this lag in all of them.

Similar questions has been asked a several times on SO, but are unanswered.


回答1:


The problem was, I was doing everything in the UI thread which was making it take a long time. Do everything in AsyncTask and things will work out fine.

For beginners, this link describes how could you do it in AsyncTask.

Define a class which extends the AsyncTask:

public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
    Integer track = 0;
    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = new ProgressDialog(PlayVideo.this);
        dialog.setMessage("Loading, Please Wait...");
        dialog.setCancelable(true);
        dialog.show();
    }

    protected void onProgressUpdate(final Uri... uri) {

        try {

            media=new MediaController(PlayVideo.this);
            video.setMediaController(media);
            media.setPrevNextListeners(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // next button clicked

                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    finish();
                }
            });
            media.show(10000);

            video.setVideoURI(uri[0]);
            video.requestFocus();
            video.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer arg0) {
                    video.start();
                    dialog.dismiss();
                }
            });


        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            Uri uri = Uri.parse(params[0]);

            publishProgress(uri);
        } catch (Exception e) {
            e.printStackTrace();

        }

        return null;
    }

}

Then, in the onCreate() method, just call the execute() method of this BackgroundAsynchTask:

video = (VideoView) findViewById(R.id.video);
new BackgroundAsyncTask().execute("link-to-the-video");


来源:https://stackoverflow.com/questions/36055649/streaming-videos-on-videoview

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