Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

android - Get the progress time of the video played under videoview?

I need to get the progress time of the video that is played in "VideoView"(ie the time shown in the left hand side in progress bar).

Any help will really be appreciated. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the Duration of the Video by mVideoView.getDuration(), set the Progress bar to 0 initially and then get the currentProgress of Video by mVideoView.getCurrentPosition(); and increase the Progress Bar status based on the CurrentProgress of Video in Percentage(%) by (current * 100 / duration). I tried it out using AsyncTask checkout this complete Example.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...