【问题标题】:How to play a video from url using videoview smoothly?如何使用videoview流畅地播放来自url的视频?
【发布时间】:2013-06-28 13:56:18
【问题描述】:

我有一个有 VideoView 的活动。它正在播放来自 url 的视频。我正在做的是为了玩得顺畅,我在活动开始时放了ProgressDialog。并在onPreparedListener 中解散它,以便它可以流畅地播放。但它仍然没有帮助。视频播放就像播放 10-20 秒并停止 5-10 秒,它会继续播放。我在 Google play Workout Trainer 上看到了一个应用程序,如果用户开始观看视频,它会显示一个 horizontal progress bar,它会缓冲视频,然后无论是慢速连接还是 WI-FI 都可以顺利播放。它只需要在开始视频之前完成progressbar。我想知道如何在我的应用程序中实现相同的功能?

我在做什么如下:

    public class StartExerciseActivity extends Activity {


    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.show_exercise);
            tileTextView = (TextView) findViewById(R.id.titleText);
            timerTextView = (TextView) findViewById(R.id.timertxt);
            timerRemainTextView = (TextView) findViewById(R.id.timeremaintxt);
            videoView = (VideoView) findViewById(R.id.video_view);
            stopButton = (Button) findViewById(R.id.stopbut);
            pauseButton = (Button) findViewById(R.id.pausesbut);



    progressDialog = ProgressDialog.show(StartExerciseActivity.this, "",
                    "Buffering video...", true);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);

            tileTextView.setText(Constant.NEWS_TITLE);
            timerTextView.setText(Constant.VIDEO_TIME);
            video_url = Constant.VIDEO_NAME;




            try {

                Uri video = Uri.parse(video_url);

                // videoView.setMediaController(mediaController);
                videoView.setVideoURI(video);

                videoView.setOnPreparedListener(new OnPreparedListener() {
                    public void onPrepared(MediaPlayer mp) {
                        progressDialog.dismiss();
                        videoView.start();
                        updateSeekProgress();


                        RelativeLayout.LayoutParams videoviewlp = new RelativeLayout.LayoutParams(400, 400);
                        videoviewlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
                        videoviewlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
                        videoView.setLayoutParams(videoviewlp);
                        videoView.invalidate();


    //                  DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //                   android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
    //                   params.width =  metrics.widthPixels;
    //                   params.height = metrics.heightPixels;
    //                   params.leftMargin = 0;
    //                   videoView.setLayoutParams(params);
                    }
                });

            } catch (Exception e) {
                progressDialog.dismiss();
                System.out.println("Video Play Error :" + e.getMessage());
            }
 pauseButton.setOnClickListener(new View.OnClickListener() {


                public void onClick(View arg0) {
                    if (count == 0) {
                        videoView.pause();
                        count = 1;
                        pauseButton.setBackgroundResource(R.drawable.playbut);


                    } else if (count == 1) {
                        videoView.start();

                        pauseButton.setBackgroundResource(R.drawable.pausebut);
                        count = 0;
                    }
                }
            });

            stopButton.setOnClickListener(new View.OnClickListener() {


                public void onClick(View arg0) {
                    videoView.stopPlayback();
                    pauseButton.setBackgroundResource(R.drawable.playbut);
                }
            });

        }

请帮忙。谢谢。

【问题讨论】:

  • 你使用AsyncTask和后台进程放在doInBackground中。

标签: android progressdialog android-videoview


【解决方案1】:

//放入oncreate
新的 DownloadXML().execute();
//放在oncreate之外

private class DownloadXML extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Create a progressbar
                pDialog = new ProgressDialog(StartExerciseActivity.this);
                // Set progressbar title
                pDialog.setTitle("Wait");
                // Set progressbar message
                pDialog.setMessage("Loading...");
                pDialog.setIndeterminate(false);
                // Show progressbar
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
            videoView.setVideoURI(Uri.parse("http://commonsware.com/misc/test2.3gp"));
                //videoView.setVideoURI(Uri.parse(videofilename));
                videoView.requestFocus();
                videoView.setMediaController(new MediaController(this));        

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

            }

            @Override
            protected void onPostExecute(Void args) {

                // Close progressbar
                pDialog.dismiss();
                videoView.start();
            }
        }

【讨论】:

  • 我想要的有点不同。如果用户将停止视频,它会再次需要时间来缓冲。我不想要的。视频缓冲后,无需再次缓冲。我已经提到了一个应用程序 Workout Trainer 的链接。请查看应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多