[英]duration of a video on Android
in this code I try to get the duration of a video with the method getDuration()
but it doesn't work and it return always -1.在这段代码中,我尝试使用
getDuration()
方法getDuration()
视频的持续时间,但它不起作用,并且始终返回 -1。 What is wrong?怎么了? Could I get the duration in another way?(The video is an mp4 file putted in the directory raw)
我可以用另一种方式获得持续时间吗?(视频是放在目录 raw 中的 mp4 文件)
videoPlayer = (VideoView) this.findViewById(R.id.videoView2);
videoPlayer.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video3);
int duration = videoPlayer.getDuration();
Copied from getDuration in videoView:从 videoView 中的 getDuration 复制:
public int getDuration(){
if (isInPlaybackState()) {
return mMediaPlayer.getDuration();
}
return -1;
}
So from what I see, you cannot get the correct duration if the video isn't playing.因此,据我所知,如果视频没有播放,您将无法获得正确的持续时间。 So a possible solution is to start the playback, get the duration to then stop it and restart the video.
所以一个可能的解决方案是开始播放,获取持续时间,然后停止并重新启动视频。
Call getDuration inside setOnPreparedListener在 setOnPreparedListener 中调用 getDuration
videoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
long duration = videoPlayer.getDuration();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.