滑动ViewPager太快时视频卡住(冻结)

时间:2019-03-20 05:59:55

标签: android android-viewpager android-videoview

我在快速滑动查看寻呼机时遇到视频冻结的问题。

问题:

我有一个 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还仅包含文本数组,并且滑动文本i m以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>

因此,任何高级帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

最后,我使用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();
}

onPageSelected的{​​{1}}方法中,您需要取消前一个ViewPager。这是最重要的事情,否则将无法正常工作。

countDownTimer