2011-10-18 14 views

Odpowiedz

32

można uzyskać w czasie trwania wideo przez mVideoView.getDuration() ustawić pasek postępu na 0 początkowo, a następnie uzyskać currentProgress z wideo przez mVideoView.getCurrentPosition(); i zwiększyć stan pasek postępu opartego na CurrentProgress of Video w procentach (%) przez (current * 100/duration) . Wypróbowałem to przy użyciu AsyncTask realizacji tego kompletnego przykładu.

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<VideoView android:id="@+id/my_Video_View" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 

    <ProgressBar android:layout_alignParentBottom="true" 
    style="@android:style/Widget.ProgressBar.Horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="10dp" 
    android:id="@+id/Progressbar"/> 
</RelativeLayout> 

VideoPlayActivity.java

public class VideoPlayActivity extends Activity { 

    ProgressBar mProgressBar; 
    VideoView mVideoView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String path = Environment.getExternalStorageDirectory().toString(); 
     String filename = "/hr.3gp"; 

     mProgressBar = (ProgressBar) findViewById(R.id.Progressbar); 
     mProgressBar.setProgress(0); 
     mProgressBar.setMax(100); 

     mVideoView = (VideoView) findViewById(R.id.my_Video_View); 
     mVideoView.setVideoURI(Uri.parse(path+filename)); 
     new MyAsync().execute(); 
    } 

    private class MyAsync extends AsyncTask<Void, Integer, Void> 
    { 
     int duration = 0; 
     int current = 0; 
     @Override 
     protected Void doInBackground(Void... params) { 

      mVideoView.start(); 
      mVideoView.setOnPreparedListener(new OnPreparedListener() { 

       public void onPrepared(MediaPlayer mp) { 
        duration = mVideoView.getDuration(); 
       } 
      }); 

      do { 
       current = mVideoView.getCurrentPosition(); 
       System.out.println("duration - " + duration + " current- " 
         + current); 
       try { 
        publishProgress((int) (current * 100/duration)); 
        if(mProgressBar.getProgress() >= 100){ 
         break; 
        } 
       } catch (Exception e) { 
       } 
      } while (mProgressBar.getProgress() <= 100); 

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      super.onProgressUpdate(values); 
      System.out.println(values[0]); 
      mProgressBar.setProgress(values[0]); 
     } 
    } 
} 
+4

czy naprawdę możesz wywołać 'mVideoView.start();' w 'doInBackground'? nie ma wyjątku wrongUiThread? –

+2

cześć Lalit. Co się stanie, jeśli użytkownik wyjdzie z VideoView lub zamknie go? W jaki sposób można zapobiec nieskończonemu wykonywaniu tego zadania AsyncTask? – codeshark

+0

@ użytkownik-android, który możesz nazwać metodą 'cancel()' na twoim AsyncTask –

4

Możesz użyć getCurrentPosition() i getDuration(), aby obliczyć postęp.

+0

ale kiedy zadzwonić tych metod ?, jak muszę śledzić to co sekundę. – NarendraJi

+0

@ mr-narendra, Możesz odnieść się do odpowiedzi Nolesha i Aleksieja Timoszienki powyżej z timerem lub senem wątku ustawionym na 1000 ms. – Karthik

+0

Osiągnąłem to dzięki pomocy 'handler'. – NarendraJi

5

wątek można użyć do tego celu jak poniżej:

videoView.start(); 
    videoView.setOnPreparedListener(new OnPreparedListener() { 
     public void onPrepared(MediaPlayer mp) { 
      running = true; 
      final int duration = videoView.getDuration(); 

      new Thread(new Runnable() { 
       public void run() { 
        do{       
         textView.post(new Runnable() { 
         public void run() { 
          int time = (duration - videoView.getCurrentPosition())/1000; 
          textView.setText(time+""); 
         } 
         }); 
         try { 
          Thread.sleep(500); 
         } catch (InterruptedException e) {          
          e.printStackTrace(); 
         } 
         if(!running) break; 
        } 
        while(videoView.getCurrentPosition()<duration); 
       } 
      }).start();      
     } 
    }); 
+0

, co powoduje wyciek w widoku tekstowym – isudansh

1

osiągnąłem to taki sposób

private void playVideo() { 
    setViewNotVisibility(); 

    final String videoSource = "android.resource://" + getPackageName() + File.separator + R.raw.moviiegood; 
    videoView.setKeepScreenOn(true); 
    videoView.setVideoURI(Uri.parse(videoSource)); 
    videoView.setMediaController(null); 
    videoView.setOnCompletionListener(myVideoViewCompletionListener); 
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
             @Override 
             public void onPrepared(MediaPlayer mp) { 
              startVideo(); 
              setDuration(); 
              timerCounter(); 
             } 
            } 

    ); 
} 

private Timer timer; 
private void timerCounter(){ 
    timer = new Timer(); 
    TimerTask task = new TimerTask() { 
     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        updateUI(); 
       } 
      }); 
     } 
    }; 
    timer.schedule(task, 0, 1000); 
} 

private int duration = 0; 

private void setDuration(){ 
    duration = videoView.getDuration(); 
} 

private void updateUI(){ 
    if (pbVideoView.getProgress() >= 100) { 
     timer.cancel(); 
    } 
    int current = videoView.getCurrentPosition(); 
    int progress = current * 100/duration; 
    pbVideoView.setProgress(progress); 
} 

Jeśli masz pytania nie wahaj się zapytać

Powiązane problemy