本文主要是介绍使用饺子播放器和ViewPager打造一个视频详情页的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JiaoZiVideoPlayer使用的是饺子播放器。官方介绍是高度自定义的安卓视频框架,这是Git地址。
·如果引入失败,检查是否添加了java8的配置
·activity的xml布局如下
·效果图
我是直接下载源码引入依赖的的
implementation project(':jiaozivideoplayer')
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.support:design:27.1.0'
public class MPFOURActivity extends AppCompatActivity {private JzvdStd jzvdStd;Jzvd.JZAutoFullscreenListener fullscreenListener;SensorManager mSensorManager;private ViewPager mViewPager;private TabLayout mTabLayout;private MyMedFragmentAdapter medFragmentAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_mpfour);initView();setVideo();setListener();}private void initView(){getSupportActionBar().setDisplayHomeAsUpEnabled(true);getSupportActionBar().setDisplayShowHomeEnabled(true);getSupportActionBar().setDisplayShowTitleEnabled(true);getSupportActionBar().setDisplayUseLogoEnabled(false);jzvdStd=findViewById(R.id.videoplayer);mTabLayout=findViewById(R.id.comment_tab);mViewPager=findViewById(R.id.comment_like);medFragmentAdapter=new MyMedFragmentAdapter(getSupportFragmentManager());mViewPager.setAdapter(medFragmentAdapter);mTabLayout.setupWithViewPager(mViewPager);}private void setVideo(){jzvdStd.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4", "饺子闭眼睛",jzvdStd.SCREEN_NORMAL/*,JzvdStd.SCREEN_STATE_ON*/);Glide.with(this).load("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640").into(jzvdStd.thumbImageView);}private void setDefinition(){/*** 给你要展示的视频准备多套清晰度的地址来调用* 自己写清晰度切换的UI*/}private void setListener(){fullscreenListener=new Jzvd.JZAutoFullscreenListener();mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);}@Overridepublic void onBackPressed() {if(Jzvd.backPress()){return;}super.onBackPressed();}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case android.R.id.home:finish();break;}return super.onOptionsItemSelected(item);}@Overrideprotected void onPause() {super.onPause();mSensorManager.unregisterListener(fullscreenListener);Jzvd.clearSavedProgress(this, null);Jzvd.goOnPlayOnPause();}@Overrideprotected void onResume() {super.onResume();Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);mSensorManager.registerListener(fullscreenListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);Jzvd.goOnPlayOnResume();}
}
方法名字写的比较明显了,就不加注释了
然后是Adapter里的代码
public class MyMedFragmentAdapter extends FragmentPagerAdapter {private String[] mTitles = new String[]{"相关", "评论"};public MyMedFragmentAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int position) {if (position == 1) {return new MedFragment2();}return new MedFragment1();}@Overridepublic int getCount() {return mTitles.length;}//设置tab的标题@Nullable@Overridepublic CharSequence getPageTitle(int position) {return mTitles[position];}
}
比较适合入门的例子,给自己做一个记录。
这篇关于使用饺子播放器和ViewPager打造一个视频详情页的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!