快速滑动 ViewPager 时视频卡住(冻结)
Video getting stuck(freeze) while swipe ViewPager too fast
我遇到了视频在快速滑动查看寻呼机时冻结的问题。
问题:
我有一个 webm 扩展类型的视频(12 秒)并在 VideoView 中播放。
从该视频中,我从该视频获取当前位置并以 4 秒的间隔暂停该视频。为此,我正在使用处理程序,当我缓慢滑动时它工作正常,但当我快速滑动时视频卡住(冻结)并且无法正常工作。
代码:
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
currentPosition = position;
if (viewPager.getCurrentItem() == 0) {
videoPlayPause(0);
}
if (viewPager.getCurrentItem() == 1) {
videoPlayPause(5);
}
if (viewPager.getCurrentItem() == 2) {
videoPlayPause(9);
}
}
};
void playVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.welcome;
autoFitVideoView.setVideoURI(Uri.parse(path));
autoFitVideoView.requestFocus();
autoFitVideoView.setBackgroundColor(Color.WHITE);
autoFitVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
autoFitVideoView.setBackgroundColor(Color.TRANSPARENT);
}
});
}
public void videoPlayPause(final int seekto) {
autoFitVideoView.seekTo(seekto * 1000);
autoFitVideoView.start();
autoFitVideoView.post(new Runnable() {
@Override
public void run() {
videoCurrentPosition = autoFitVideoView.getCurrentPosition();
if (currentPosition == 0) {
if (videoCurrentPosition == 0) {
pauseVideo(3000);
}
}
if (currentPosition == 1) {
if (videoCurrentPosition == 5000) {
pauseVideo(3000);
}
}
if (currentPosition == 2) {
if (videoCurrentPosition == 9000) {
pauseVideo(4000);
}
}
}
});
}
void pauseVideo(long delayTime) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
autoFitVideoView.pause();
}
}, delayTime);
}
此外,ViewPager 仅包含文本数组和滑动文本,我正在显示时间间隔为 4 秒的视频。但是视频卡住了。
XML:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
android:overScrollMode="never"
app:layout_constraintBottom_toTopOf="@+id/txt_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title">
<in.hammerapps.view.CustomViewPager
android:id="@+id/viewPager"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"></in.hammerapps.view.CustomViewPager>
<VideoView
android:id="@+id/videoView"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
因此,我们将不胜感激任何高级帮助!
最后我用 CountDownTimer
而不是 Handler
解决了这个问题。
CountDownTimer countDownTimer; // This is globally declared
void pauseVideo(final long delayTime) {
countDownTimer = new CountDownTimer(delayTime, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if (autoFitVideoView.isPlaying()) {
autoFitVideoView.pause();
}
}
};
countDownTimer.start();
}
在ViewPager
的onPageSelected
方法中需要取消之前的countDownTimer
。这是您需要做的最重要的事情,否则它将无法工作。
@Override
public void onPageSelected(int position) {
currentPosition = position;
if (countDownTimer != null) {
countDownTimer.cancel(); // Most important thing
}
if (viewPager.getCurrentItem() == 0) {
videoPlayPause(0);
}
if (viewPager.getCurrentItem() == 1) {
videoPlayPause(5);
}
if (viewPager.getCurrentItem() == 2) {
videoPlayPause(9);
}
}
我遇到了视频在快速滑动查看寻呼机时冻结的问题。
问题:
我有一个 webm 扩展类型的视频(12 秒)并在 VideoView 中播放。
从该视频中,我从该视频获取当前位置并以 4 秒的间隔暂停该视频。为此,我正在使用处理程序,当我缓慢滑动时它工作正常,但当我快速滑动时视频卡住(冻结)并且无法正常工作。
代码:
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
currentPosition = position;
if (viewPager.getCurrentItem() == 0) {
videoPlayPause(0);
}
if (viewPager.getCurrentItem() == 1) {
videoPlayPause(5);
}
if (viewPager.getCurrentItem() == 2) {
videoPlayPause(9);
}
}
};
void playVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.welcome;
autoFitVideoView.setVideoURI(Uri.parse(path));
autoFitVideoView.requestFocus();
autoFitVideoView.setBackgroundColor(Color.WHITE);
autoFitVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
autoFitVideoView.setBackgroundColor(Color.TRANSPARENT);
}
});
}
public void videoPlayPause(final int seekto) {
autoFitVideoView.seekTo(seekto * 1000);
autoFitVideoView.start();
autoFitVideoView.post(new Runnable() {
@Override
public void run() {
videoCurrentPosition = autoFitVideoView.getCurrentPosition();
if (currentPosition == 0) {
if (videoCurrentPosition == 0) {
pauseVideo(3000);
}
}
if (currentPosition == 1) {
if (videoCurrentPosition == 5000) {
pauseVideo(3000);
}
}
if (currentPosition == 2) {
if (videoCurrentPosition == 9000) {
pauseVideo(4000);
}
}
}
});
}
void pauseVideo(long delayTime) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
autoFitVideoView.pause();
}
}, delayTime);
}
此外,ViewPager 仅包含文本数组和滑动文本,我正在显示时间间隔为 4 秒的视频。但是视频卡住了。
XML:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
android:overScrollMode="never"
app:layout_constraintBottom_toTopOf="@+id/txt_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title">
<in.hammerapps.view.CustomViewPager
android:id="@+id/viewPager"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"></in.hammerapps.view.CustomViewPager>
<VideoView
android:id="@+id/videoView"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
因此,我们将不胜感激任何高级帮助!
最后我用 CountDownTimer
而不是 Handler
解决了这个问题。
CountDownTimer countDownTimer; // This is globally declared
void pauseVideo(final long delayTime) {
countDownTimer = new CountDownTimer(delayTime, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if (autoFitVideoView.isPlaying()) {
autoFitVideoView.pause();
}
}
};
countDownTimer.start();
}
在ViewPager
的onPageSelected
方法中需要取消之前的countDownTimer
。这是您需要做的最重要的事情,否则它将无法工作。
@Override
public void onPageSelected(int position) {
currentPosition = position;
if (countDownTimer != null) {
countDownTimer.cancel(); // Most important thing
}
if (viewPager.getCurrentItem() == 0) {
videoPlayPause(0);
}
if (viewPager.getCurrentItem() == 1) {
videoPlayPause(5);
}
if (viewPager.getCurrentItem() == 2) {
videoPlayPause(9);
}
}