gpt4 book ai didi

android - 获取videoview下播放视频的进度时间?

转载 作者:IT老高 更新时间:2023-10-28 23:09:06 25 4
gpt4 key购买 nike

我需要获取“VideoView”中播放的视频的进度时间(即进度条左侧显示的时间)。

任何帮助将不胜感激。 谢谢。

最佳答案

您可以通过mVideoView.getDuration()获取视频的时长,初始设置进度条为0,然后通过mVideoView.getCurrentPosition()获取视频的currentProgress; 并根据当前视频的百分比 (%) 通过 (current * 100/duration) 增加进度条状态。我使用 AsyncTask 进行了尝试,查看这个完整的示例。

ma​​in.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]);
}
}
}

关于android - 获取videoview下播放视频的进度时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7802645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com