如何从URL使用videoview流畅播放视频? [英] How to play a video from url using videoview smoothly?

查看:652
本文介绍了如何从URL使用videoview流畅播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中有一个 VideoView 的活动。这是一个从URL播放视频。我在做什么是玩顺利,我已经把 ProgressDialog 在活动开始。或解散它里面在preparedListener ,这样它会发挥出色,顺利。但仍然是没有帮助。播放视频时就像打10-20秒,并停止对5-10sec和它不断前进。我看到的谷歌播放<一个应用程序href="https://play.google.com/store/apps/details?id=com.skimble.workouts&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5za2ltYmxlLndvcmtvdXRzIl0."相对=nofollow>训练器的其中如果用户将开始看到视频的呈现出水平进度条这缓冲的视频,然后流畅播放无论是一个缓慢的连接或Wi-Fi。它只需要完成的进度启动视频前。我想知道如何实现在我的应用是一回事吗?

I have an activity which has a VideoView . It's playing video from url. What I am doing is for playing smoothly, I have put a ProgressDialog on the start of the activity. And dismisses it inside onPreparedListener so that it would play well and smoothly. But still It's not helping. The video is playing like playing for 10-20 sec and stops for 5-10sec and it keeps going. I have seen an application on Google play Workout Trainer in which if user will start to see the video its showing a horizontal progress bar which buffered the video and then play it smoothly whether it is a slow connection or WI-FI. It requires only to complete that progressbar before starting the video. I want to know how to implement the same thing in my application?

我在做什么是如下:

    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);
                }
            });

        }

请帮忙。谢谢你。

推荐答案

//投入的OnCreate
 新DownloadXML()执行();
//摆在OnCreate中之外

//put in oncreate
new DownloadXML().execute();
//put in outside of 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();
            }
        }

这篇关于如何从URL使用videoview流畅播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆