为什么Android中的VideoView花费太多时间从http链接加载和播放视频?

时间:2014-03-12 07:39:09

标签: android video-streaming android-videoview

以下是我的代码:

  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 个答案:

答案 0 :(得分:0)

看看这个:

Play http videos in VideoView

这是我的代码:

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) {

}
});

}

由于

答案 1 :(得分:-1)

不要在UI线程中执行所有操作,因为它需要花费大量时间。在AsyncTask中完成所有事情,事情会顺利进行。

对于初学者,this link介绍了如何在AsyncTask中执行此操作。

定义一个扩展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;
    }

}

然后,在onCreate()方法中,只需调用此execute()的{​​{1}}方法:

BackgroundAsynchTask