快速滑动(滚动)时,视频会在带有ViewPager的VideoView中冻结

时间:2019-03-28 06:17:43

标签: 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>

我们将不胜感激。

0 个答案:

没有答案