2013-06-28 60 views
3

我有一項活動,它有一個VideoView。它正在播放來自網址的視頻。我所做的是爲了順利進行,我在活動開始時放了一個ProgressDialog。並在onPreparedListener內解散它,以便它能夠順利播放。但仍然沒有幫助。視頻播放時間爲10-20秒,停止5-10秒後繼續播放。我在Google play Workout Trainer上看到過一個應用程序,其中如果用戶將開始看到視頻,其中顯示緩衝視頻的horizontal progress bar,然後無論是慢速連接還是WI-FI,都可以順利播放。在開始視頻之前,它只需要完成progressbar。我想知道如何在我的應用程序中實現同樣的事情?如何順利使用videoview從網址中播放視頻?

我在做什麼下面是:

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

     } 

請幫助。謝謝。

+0

您使用的AsyncTask外和後臺進程放在doInBackground。 – AnilPatel

回答

2

//放在上面
new 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(); 
      } 
     } 
+0

我想要的是有點不同。如果用戶將停止視頻,它將再次需要時間來緩衝。我不想要。一旦視頻緩衝,不需要再次緩衝。我已經提到了一個應用程序Workout Trainer的鏈接。請參閱應用程序。 – TheLittleNaruto

+0

這不能是我的答案。但很好的代碼,所以upvoted +1。 – TheLittleNaruto