Android 使用定时器同步视频
我正在使用 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();
I'm using Android to construct a video player using the VideoView.
I've managed to get a video player running and now I'm setting a counter using chronometer that starts ticking when I select the file.
However, the video clip takes a few seconds to start running while the timer has already begun counting a few seconds when the clip starts.
How can I code to sync the counter with the media file?
I've checked around but can't seem to find an answer.
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不用担心,我找到了一种在 Chronometer 中使用 VideoView getCurrentPosition() 的方法。
现在它总是能看到正确的当前时间并且更容易检查。
No worries, I found a way to use VideoView getCurrentPosition() in the Chronometer.
Now it always sees the correct current time and is easier to keep check of.
我会为此付出两分钱,因为它可能会帮助其他人。
您可以设置 onPreparedListener 来获取 MediaPlayer。
MediaPlayer 具有 onInfoListener,可以让您知道媒体何时被缓冲,甚至是实际渲染的第一帧 MEDIA_INFO_VIDEO_RENDERING_START(仅限 API 17+)。
我使用它来更新重写的 VideoView 类中的本地状态变量(例如:enum VideoPlayerState (BUFFERING, PLAYING, STOPPED, OPEN, PAUSED))。
您可以重写其设置器以在状态更改时获取回调。
该回调可以启动/暂停您的计时器。
您还可以在 VideoView 上重写这些方法来切换状态:
希望它有帮助。
I'll give my two cents on this as it may help others.
You can setup onPreparedListener where you will be able to get hold of MediaPlayer.
MediaPlayer has onInfoListener that will let you know when the media is being buffered, or even the first frame actually being rendered MEDIA_INFO_VIDEO_RENDERING_START (only API 17+).
I'm using this to update the local state variable (something like: enum VideoPlayerState (BUFFERING, PLAYING, STOPPED, OPEN, PAUSED)) in the overridden VideoView class.
You can override its setter to get a callback when the state is changing.
That callback can kick off/pause your timer.
You can also override these methods on the VideoView to switch the state:
Hope it helps.