我正在使用 Android 使用VideoView构建视频播放器。我已经设法让视频播放器运行,现在我正在使用计时器设置一个计数器,当我选择文件时它开始滴答作响。
但是,视频剪辑需要几秒钟才能开始运行,而计时器在剪辑开始时已经开始计时几秒钟。
如何编码以将计数器与媒体文件同步?
我已经检查过,但似乎无法找到答案。
video = (VideoView) findViewById(R.id.video);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
reset = (Button) findViewById(R.id.reset);
Chronometer counter = (Chronometer) findViewById(R.id.chrono);
startTime = SystemClock.elapsedRealtime();
time = (TextView) findViewById(R.id.time);
try {
video.setVideoPath(link);
video.start();
video.requestFocus();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
video.start();
video.requestFocus();
}
});
pause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
video.pause();
}
});
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
video.stopPlayback();
video.setVideoPath(link);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
});
reset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
video.seekTo(0);
video.start();
video.requestFocus();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
});
counter.setOnChronometerTickListener(new OnChronometerTickListener(){
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
duration = video.getDuration();
if (countUp % 60 <= 9) {
countText = (countUp / 60) + ":0" + (countUp % 60);
} else {
countText = (countUp / 60) + ":" + (countUp % 60);
}
if (duration % 60000 <= 9) {
durationText = (duration / 60000) + ":0" + (duration % 60000);
} else {
durationText = (duration / 60000) + ":" + (duration % 60000);
}
time.setText(countText + " / " + durationText);
}
});
counter.start();