Why VideoView in android is taking too much time t

2019-03-19 06:52发布

Below is my Code :

  package com.example.videoplayer;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.widget.MediaController;
  import android.widget.VideoView;

public class VideoPlayerActivity extends Activity {
    String TAG = "com.example.VideoPlayer";
    ProgressDialog progDailog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        final VideoView videoView = (VideoView) findViewById(R.id.videoView1);
        videoView.setVideoPath("http://www.ebookfrenzy.com/android_book/movie.mp4");

        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);

        videoView.setOnPreparedListener(new 
                MediaPlayer.OnPreparedListener()  {
            @Override
            public void onPrepared(MediaPlayer mp) {
                progDailog.dismiss();
                Log.i(TAG, "Duration = " + videoView.getDuration());
            }
        });

        videoView.start();
        progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", `enter code here`true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.video_player, menu);
        return true;
    }

}

2条回答
爷的心禁止访问
2楼-- · 2019-03-19 07:09

Check out this :

Play http videos in VideoView

This is my code :

public static void getVideoFromHttp(String urlPath) {

try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);

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

}

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

}
});

mVideoview.setOnCompletionListener(new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

}
});

}

Thanks

查看更多
再贱就再见
3楼-- · 2019-03-19 07:27

Don't do everything in the UI thread because it takes a lot of 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");
查看更多
登录 后发表回答