我必须在视图寻呼机内播放3个视频,我使用PagerAdapter转到不同的页面。 我遇到了一些问题:第一次,第一个视频没有渲染,它显示黑屏。 我的第二个视频仅在访问时第一次运行。当我访问该页面时,其他视频会再次运行。以下是我正在使用的课程
enter code here
public class VideoPagerAdapter extends PagerAdapter {
Context context;
static int[] arrayvid;
private VideoView videoView;
LayoutInflater inflater;
View layout;
TextView message;
public VideoPagerAdapter(Context context, int[] arrayvid) {
this.context = context;
this.arrayvid = arrayvid;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return arrayvid.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layout = inflater.inflate(R.layout.video_layout, container, false);
videoView = (VideoView)layout.findViewById(R.id.video_view);
message = (TextView)layout.findViewById(R.id.message);
MediaController mediaController = new MediaController(context, false);
mediaController.setAnchorView(videoView);
((ViewPager) container).addView(layout);
return layout;
}
private Uri getPath(int id) {
Log.i("Video", "num" + id);
return Uri.parse("android.resource://com.example.videoviewpager/raw/video_" + id);
}
public void play(int position) {
videoView.setVideoURI(getPath(position+1));
videoView.requestFocus();
videoView.start();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(View)object;
}
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
PagerAdapter adapter;
private int[] ids = {R.raw.video_1, R.raw.video_2,R.raw.video_3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.video_pager);
// Pass results to ViewPagerAdapter Class
adapter = new VideoPagerAdapter(MainActivity.this,ids);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
//((VideoPagerAdapter) adapter).play(0);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(position==0){
((VideoPagerAdapter) adapter).play(position);
}
if(position==1){
((VideoPagerAdapter) adapter).play(position);
}
if(position==2){
((VideoPagerAdapter) adapter).play(position);
}
}
//
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private Uri getPath(int id) {
Log.i("Video", "num" + id);
return Uri.parse("android.resource://com.example.videoviewpager/raw/video_" + id);
}
}
答案 0 :(得分:0)
在viewpager中显示视频的最简单方法是使用FragmentStatePagerAdapter
代替PagerAdapter
。
在getItem
的{{1}}下,创建片段实例,您将在其中显示视频。视频的播放和暂停/停止功能应在Fragment的FragmentStatePagerAdapter
& onStart
生命周期方法