欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)

程序员文章站 2022-04-20 08:01:56
上代码依赖 compile 'fm.jiecao:jiecaovideoplayer:4.8.3' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.zhy:base-adapter:3.0.3' 注意的是在AndroidManifest...

上代码
依赖

 compile 'fm.jiecao:jiecaovideoplayer:4.8.3'
 compile 'com.github.bumptech.glide:glide:3.7.0'
 compile 'com.zhy:base-adapter:3.0.3'
 <!-- 联网权限 -->
    <uses-permission android:name="android.permission.INTERNET" />

注意的是在AndroidManifest.xml添加
android:configChanges=“keyboardHidden|orientation|screenSize”
懂得都懂在横竖屏切换时保存状态不然切换横屏的时候会出现黑屏现象

 <!--android:configChanges="keyboardHidden|orientation|screenSize"-->
           
  <activity android:name=".MainActivity2"
        android:configChanges="keyboardHidden|orientation|screenSize">
 </activity>

在根目录添加
Android  视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)
布局添加

 <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
        android:id="@+id/player_list_video"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        tools:ignore="MissingConstraints" />

在Activity里面添加监听我们返回停止视频

 @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

然后我们需要获取视频的第一帧作为视频的缩略图

    public static Bitmap getNetVideoBitmap(String videoUrl) {
        Bitmap bitmap = null;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, new HashMap());
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;
    }

接下来就是播放视频了

String videoUrl="http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4";
JCVideoPlayerStandard player = (JCVideoPlayerStandard) findViewById(R.id.player_list_video);
        player.setUp(videoUrl, JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
        Bitmap bitmap = getNetVideoBitmap(videoUrl);
        player.thumbImageView.setImageBitmap(bitmap);//对应的ImageView赋值图片一定要用thumbImageView才能获取设置上

Android  视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)
然后就是防今日头条视频然后滑动自动播放
适配器

public class VideoAdapter extends CommonAdapter<String> {
    private Context mContext;
    private String videoUrl="http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4";
    private Bitmap bitmap;
    private MediaMetadataRetriever retriever;

    public VideoAdapter(Context context, List<String> datas, int layoutId) {
        super(context, layoutId, datas);
        this.mContext = context;
    }

    @Override
    protected void convert(ViewHolder viewHolder, String url, int position) {
        JCVideoPlayerStandard player = viewHolder.getView(R.id.player_list_video);
        if (player != null) {
            player.release();
        }
        boolean setUp = player.setUp(url, JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
      /*  retriever = new MediaMetadataRetriever();   //获取视频第一帧图片有真实接口后可进行遍历获取暂时定死

        //根据url获取缩略图
        retriever.setDataSource(videoUrl, new HashMap());
        //获得第一帧图片
        bitmap = retriever.getFrameAtTime();
        player.thumbImageView.setImageBitmap(bitmap);//对应的ImageView赋值图片*/
        // Glide.with(mContext).load(videoUrl).into(player.thumbImageView);
         if (setUp) {
            Glide.with(mContext).load("http://a4.att.hudong.com/05/71/01300000057455120185716259013.jpg").into(player.thumbImageView);
        }
    }
}

适配器布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
        android:id="@+id/player_list_video"
        android:layout_width="match_parent"
        android:layout_height="190dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp">

        <ImageView
            android:id="@+id/img_video_icon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="12dp"
            android:src="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/tv_video_userName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:layout_toRightOf="@id/img_video_icon"
            android:text="空城旧梦" />

        <TextView
            android:id="@+id/tv_video_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="12dp"
            android:drawablePadding="4dp"
            android:text="????21"
            android:textSize="12sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="12dp"
            android:layout_toLeftOf="@id/tv_video_comment"
            android:text="3万次播放" />
    </RelativeLayout>
</LinearLayout>

Activity代码与布局

public class MainActivity2 extends AppCompatActivity {
    private String videoUrl = "http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4";
    private ListView listView;
    private ArrayList<String> datas;
    private JCVideoPlayerStandard currPlayer;
    private VideoAdapter adapter;
    private AbsListView.OnScrollListener onScrollListener;
    private int firstVisible;//当前第一个可见的item
    private int visibleCount;//当前可见的item个数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listView = (ListView) findViewById(R.id.listview);
        initDatas();
        initListener();
    }
    private void initDatas() {
        datas = new ArrayList<String>();
        for (int i = 0; i < 20; i++) {
            datas.add(videoUrl);
        }
        adapter = new VideoAdapter(MainActivity2.this, datas, R.layout.item_video);
        listView.setAdapter(adapter);
    }
    /**
     * 滑动监听
     */
    private void initListener() {
        onScrollListener = new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        break;

                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        //滑动停止自动播放视频
                        autoPlayVideo(view);
                        break;

                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisible == firstVisibleItem) {
                    return;
                }

                firstVisible = firstVisibleItem;
                visibleCount = visibleItemCount;
            }
        };

        listView.setOnScrollListener(onScrollListener);
    }
    /**
     * 滑动停止自动播放视频
     */
    private void autoPlayVideo(AbsListView view) {

        for (int i = 0; i < visibleCount; i++) {
            if (view != null && view.getChildAt(i) != null && view.getChildAt(i).findViewById(R.id.player_list_video) != null) {
                currPlayer = (JCVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.player_list_video);
                Rect rect = new Rect();
                //获取当前view 的 位置
                currPlayer.getLocalVisibleRect(rect);
                int videoheight = currPlayer.getHeight();
                if (rect.top == 0 && rect.bottom == videoheight) {
                    if (currPlayer.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL
                            || currPlayer.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
                        currPlayer.startButton.performClick();
                    }
                    return;
                }
            }
        }
        //释放其他视频资源
        JCVideoPlayer.releaseAllVideos();
    }
    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

不要忘记在res添加xml文件夹创建network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后把 android:networkSecurityConfig="@xml/network_security_config"放到AndroidManifest的里面
Android  视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)

Android  视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)
最后感谢作者查阅对象
原项目GitHub地址:
有需要可以将原项目依赖进自己的项目自定义修改更方便
此博客Demo

本文地址:https://blog.csdn.net/qq_42609613/article/details/110227580