我需要获取“VideoView”中播放的视频的进度时间(即进度条左侧显示的时间)。
任何帮助将不胜感激。
谢谢。
您可以通过以下方式获取视频的持续时间mVideoView.getDuration()
,最初将进度条设置为 0,然后通过以下方式获取视频的当前进度mVideoView.getCurrentPosition();
并根据视频的当前进度百分比(%)增加进度条状态(current * 100 / duration)
。我尝试使用AsyncTask
查看这个完整的示例。
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>
视频播放活动.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]);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)