Reputation: 558
I m getting issue with Video getting freeze while swiping view pager fast.
Problem:
I have one webm extension type video (12 seconds) and playing it in VideoView.
From that video i m getting current position from that video and pause that video at the interval of 4 seconds. For that i m using handler and it works fine when i swipe slowly but when i swipe fast the video is getting stuck (freeze) and not working properly.
Code:
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);
}
Also ViewPager contains only the text array and with swipe text i m showing video with time interval of 4 seconds. But video getting freezes.
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>
So, Any advanced help would be appreciated!
Upvotes: 0
Views: 459
Reputation: 558
Finally I solved the issue by using CountDownTimer
instead of 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();
}
In onPageSelected
method of ViewPager
you need to cancel the previous countDownTimer
. That's the most import thing you need to otherwise it will not work.
@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);
}
}
Upvotes: 1