Git Product home page Git Product logo

bluearvin / yjplay Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yangchaojiang/yjplay

0.0 2.0 0.0 92.39 MB

一个支持自定义布局和直播 手势 亮度,音量,快进,等手势 ,广告视频预览 ,清晰度切换 , 边播放缓存视频, 自定义各种数据源加载,列表播放,倍数播放 的播放器

License: Apache License 2.0

Java 94.95% HTML 4.59% Makefile 0.15% Shell 0.07% C++ 0.24%

yjplay's Introduction

yjPlay

Download

gif 显示有点卡,帧数低,实际很流畅

基于exoPlayer 自定义播放器 JPlayer支持功能:

  • 1 ExoUserPlayer 基本播放。
  • 2 GestureVideoPlayer 增加手势 亮度,音量,快进,等手势。
  • 3 ManualPlayer 默认手动播放。
  • 5 广告视频预览(轻松实现,完美切换)。
  • 6 视频清晰度切换。
  • 7 缓存视频功能。
  • 8 支持自定义多种 Okttp,Rtmp, 缓存,Cronet等协议,
  • 9 支持列表集合数据播放视频(完美切换)
  • 10 支持多种文件类型,MP4,M4A,WebM,Matroska,Ogg,WAV,MP3,MPEG-TS,MPEG-PS,FLV,ADTS (AAC),Flac,M3U8,mkv 等。
  • 11 支持网络类型 提示是否播放。
  • 12 1.5.5增加,视频加载布局, 错误布局,重播布局,提示布局自定义,更灵活实现自己布局样式。
  • 13 支持视频加载中显示模式(网速模式和百分比模式)。
  • 14 支持视频倍速播放。
  • 15 支持视频封面图(两种模式封面图)。
  • 16 1.7.0支持自定义MediaSource。
  • 17 1.7.0增加 手势 亮度调节,视频进度,音量 布局自定义。

查看1.7.0升级日志 注意以前版本:使用自定义控制布局,请重新参考新版本布局文件

一.引用类库

 repositories {
        jcenter()
        mavenCentral();
    }

dependencies {
   compile 'com.ycjiang:VideoPlayModule:1.7.0'

}

提示:无法正常引用请在 repositories{ }添加已下代码 mavenCentral(url: "https://dl.bintray.com/ycjiang/ycjiang")

二.控件属性

1.控件属性

控件自定义属性

  <chuangyuan.ycj.videolibrary.widget.VideoPlayerView
        android:id="@+id/exo_play_context_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        app:resize_mode="fit"
        app:show_timeout="3000"
        app:controller_layout_id="@layout/simple_exo_playback_control_view"
        app:player_layout_id="@layout/simple_exo_view"
        app:surface_type="texture_view"
        app:paddingEnd="0dp"
        app:paddingStart="0dp"
        app:fastforward_increment="0"
        app:rewind_increment="0"
        app:user_watermark="@mipmap/watermark_big"
        app:player_list="true"
        app:use_controller="true"
        app:player_replay_layout_id="@layout/custom_play_replay"
        app:player_error_layout_id="@layout/custom_play_error"
        app:player_hint_layout_id="@layout/custom_play_btn_hint"
        app:player_load_layout_id="@layout/custom_exo_play_load"
        app:player_gesture_audio_layout_id="@layout/custom_gesture_audio"
        app:player_gesture_bright_layout_id="@layout/custom_gesture_brightness"
        app:player_gesture_progress_layout_id="@layout/custom_gesture_pro"
         />

基本使用如下

      <chuangyuan.ycj.videolibrary.widget.VideoPlayerView
              android:id="@+id/exo_play_context_id"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:color/transparent"
              app:controller_layout_id="@layout/simple_exo_playback_control_view"
              app:player_layout_id="@layout/simple_exo_view"
              app:resize_mode="fit"
              app:surface_type="surface_view" />

2.属性说明

  • 必选
1.   player_layout_id  播放器布局, //必选
     player_layout_id 目前支持指定布局simple_exo_playback_control_view 后续版本,开放自定义使用
  • 必选
2. controller_layout_id  控制器布局`  默认有三种布局
    1.simple_exo_playback_control_view.xml  //视频封面控制布局下面,比较常规使用
    2.simple_exo_playback_list_view.xml.xml //在列表播放使用控制布局
    3.simple_exo_playback_top_view.xml.xml  //视频封面控制布局上面
  • 可选 注意: 列表播放只能选择texture_view 不能选择surface_view,详情页面播放推荐surface_view
3.    surface_type 视频渲染类型 //texture_view 和surface_view //枚举类型。默认surface_view
4.   use_controller   是否用户控制控制器  布尔类型
5.   resize_mode  视频缩放渲染显示方式一共4种 //可选  
        1.fit          //正常模式
        2.fixed_width  //保持的是视频宽度,拉伸视频高度
        3.fixed_height //保持的是视频高度,拉伸视频宽度
        4.fill          //全屏模式,拉伸视频宽高
6.   default_artwork  占位图  //可选
7.   show_timeout  控制布局隐藏时间  默认值为3秒  //可选
8.   paddingEnd,paddingStart 设置边距  默认值为0  //可选
9.   fastforward_increment  设置快进增量,以毫秒为单位。 //可选
10.  rewind_increment   设置快退增量,以毫秒为单位。  //可选
11.  user_watermark    水印图片 默认在右上角  //可选
12.  player_list      是否指定列表播放   //可选 默认 false  true 列表播放
13.  player_replay_layout_id  自定义重播布局文件
14.  player_error_layout_id   自定义错误布局文件
15.  player_hint_layout_id   自定义非wifi提示布局文件
16.  player_load_layout_id   自定义视频加载布局文件
17.  player_gesture_audio_layout_id   自定义手势音频调节布局
18.  player_gesture_bright_layout_id   自定义手势亮度调节布局
19.  player_gesture_progress_layout_id   自定义手势进度调节布局

3.修改网络对话框提示文字内容

  app.strings.xml
  <string name="exo_play_reminder">您当前网络不是wifi,是否继续观看视频</string>
  <string name="exo_play_wifi_hint_no">提示</string>

4.在功能清单声明 AndroidManifest.xml

在activity节点 加上“android:configChanges="orientation|keyboardHidden|screenSize"”
 如下实例:
        <activity android:name="chuangyuan.ycj.yjplay.MainListActivity"
         android:configChanges="orientation|keyboardHidden|screenSize"
         android:screenOrientation="portrait">

3.JAVA 代码

1 播放控制类

1.ExoUserPlayer 基本播放父类,实现基本播放,设置setPlayUri();会自动加载播放
2.GestureVideoPlayer  具有手势操作播放(调节亮度和视频进度,和音量)会自动加载播放
2.ManualPlayer  点击开始按钮播放,具有手势功能,和列表播放

2 播放代码

     //实例化播放控制类
      ManualPlayer exoPlayerManager = new ManualPlayer(this,R.id.exo_play_context_id);
     //自定义你的数据源,后面详细介绍如何自定义数据源类
      // ManualPlayer exoPlayerManager = new ManualPlayer(this,R.id.exo_play_context_id,new DataSource(this));
      //加载m3u8
      exoPlayerManager.setPlayUri("http://dlhls.cdn.zhanqi.tv/zqlive/35180_KUDhx.m3u8");
      //加载ts.文件
      exoPlayerManager.setPlayUri("http://185.73.239.15:25461/live/1/1/924.ts");
      //播放本地视频
      // exoPlayerManager.setPlayUri("/storage/emulated/0/DCIM/Camera/VID_20170717_011150.mp4");
      //下面开启多线路播放
        exoPlayerManager.setShowVideoSwitch(true); //开启切换按钮,默认关闭
       String [] test={"http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4","http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4","http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4"};
       String[] name={"超清","高清","标清"};
       exoPlayerManager.setPlaySwitchUri(test,name);
      //添加水印图片
      // exoPlayerManager.setExoPlayWatermarkImg();
       //设置视循环播放
       exoPlayerManager.setLooping(10);
       //d隐藏控制布局
       exoPlayerManager.hideControllerView();
        //隐藏进度条
       exoPlayerManager.hideSeekBar();
        //显示进度条
       exoPlayerManager.showSeekBar();
        //是否播放
       exoPlayerManager.isPlaying(); 
       //设置点击播放按钮需要处理业务
       exoPlayerManager.setOnPlayClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          Toast.makeText(MainCustomActivity.this,"定义点击播放事件",Toast.LENGTH_LONG).show();
                           //处理业务操作 完成后,
                           //方法实现setOnPlayClickListener(), 需要手动调用
                           exoPlayerManager.startPlayer();//开始播放
                      }
         }); 

1.实例化播放控制类

      ManualPlayer exoPlayerManager = new ManualPlayer(this,R.id.exo_play_context_id);
      ManualPlayer exoPlayerManager = new ManualPlayer(this,videoPlayerView);

2.自定义你的数据源,后面详细介绍如何自定义数据源类

     ManualPlayer exoPlayerManager = new ManualPlayer(this,R.id.exo_play_context_id,new DataSource(this));
     ManualPlayer exoPlayerManager = new ManualPlayer(this,videoPlayerView,new DataSource(this));

3.设置视频标题

      exoPlayerManager.setTitle("视频标题");

4.添加水印图片

     exoPlayerManager.setExoPlayWatermarkImg(R.mipmap.watermark_big);

5.设置开始播放进度

     exoPlayerManager.setPosition(1000)

6.设置封面图

       videoPlayerView.setPreviewImage(bimtap);或者 videoPlayerView.getPreviewImage())

7.设置视频路径

     exoPlayerManager.setPlayUri("http://dlhls.cdn.zhanqi.tv/zqlive/35180_KUDhx.m3u8");
     exoPlayerManager.setPlayUri(Uri.parse("http://dlhls.cdn.zhanqi.tv/zqlive/35180_KUDhx.m3u8"));
     exoPlayerManager.setPlayUri(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.h264"); //本地视频

8.设置多线路播放

      //开启多线路设置,默认关闭
      exoPlayerManager.setShowVideoSwitch(true);
      //支持List列表
      String [] test={"http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4",
      "http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4",
       http://120.25.246.21/vrMobile/travelVideo/zhejiang_xuanchuanpian.mp4"};
       String[] name={"超清","高清","标清"};
       exoPlayerManager.setPlaySwitchUri(test,name);

9.设置视频加载提示显示模式(默认LoadModelType.SPEED (网速模式))

   /**设置加载百分比显示模式**/
   exoPlayerManager.setLoadModel(LoadModelType.PERCENR);

10.设置视频倍数播放)

   //设置播放视频倍数  快放和慢放播放 小于1 慢放 大于1 快放
   exoPlayerManager.setPlaybackParameters(2f,2f);

11.广告视频预览(轻松实现)

      /**需要添加参数就行**/
      //第一个参数代表是广告视频位置索引
       exoPlayerManager.setPlayUri(0, "http://mp4.vjshi.com/2013-07-25/2013072519392517096.mp4", "http://mp4.vjshi.com/2013-11-11/1384169050648_274.mp4");       
        //如果自己在播放视频时特出处理。实现该接口回调
       //视频切换回调处理,进行布局处理,控制布局显示
             exoPlayerManager.setOnWindowListener(new VideoWindowListener() {
                 @Override
                 public void onCurrentIndex(int currentIndex, int windowCount) {
                     if (currentIndex == 0) {
                         //屏蔽控制布局
                         exoPlayerManager.hideControllerView();
                         //如果屏蔽控制布局 但是需要显示全屏按钮。手动显示,播放正常时自动还原。无需里出
                         videoPlayerView.getExoFullscreen().setVisibility(View.VISIBLE);
                     } else {
                         //恢复控制布局
                         exoPlayerManager.showControllerView();
                     }
                 }
             });
       //跳过广告视频操作
       exoPlayerManager.next();

12.设置监听回调VideoInfoListener

     exoPlayerManager.setVideoInfoListener(new VideoInfoListener() {
                   @Override
                   public void onPlayStart() {
                         //开始播放
                   }

                   @Override
                   public void onLoadingChanged() {
                             //加载变化
                   }

                   @Override
                   public void onPlayerError(ExoPlaybackException e) {
                             //加载错误
                  }

                   @Override
                   public void onPlayEnd() {
                          //播放结束
                   }
                   @Override
                   public void onRepeatModeChanged(int repeatMode) {
                       //模式变化
                   }
               });

13.覆写Activity和Fragment周期方法

            Override
            public void onResume() {
                super.onResume();
                Log.d(TAG, "onResume");
                exoPlayerManager.onResume();
            }

            @Override
            public void onPause() {
                super.onPause();
                Log.d(TAG, "onPause");
                exoPlayerManager.onPause();
            }


            @Override
            protected void onDestroy() {
                exoPlayerManager.onDestroy();
                super.onDestroy();

            }

            @Override
            public void onConfigurationChanged(Configuration newConfig) {
                exoPlayerManager.onConfigurationChanged(newConfig);//横竖屏切换
                super.onConfigurationChanged(newConfig);
            }

            @Override
            public void onBackPressed() {
            //使用播放返回键监听
             if(exoPlayerManager.onBackPressed()){
                 finish();
             }
            }

三.列表

1.列表播放,只能使用ManualPlayer,在你的VideoHolder

  • 1在列表控件使用属性 ”app:controller_layout_id="@layout/simple_exo_playback_list_view"“ //提供默列表控制布局

  • 2.player_list="true" 设置为true 开启列表模式

  • 3.demo: public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {

         private Context mContext;
         private List<String> mVideoList;
        public VideoAdapter(Context context, List<String> videoList) {
            mContext = context;
            mVideoList = videoList;
        }
    
        @Override
        public int getItemCount() {
            return mVideoList.size();
        }
        @Override
        public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_video1, parent, false);
            return new VideoViewHolder(itemView);
        }
    
        @Override
        public void onBindViewHolder(VideoViewHolder holder, int position) {
            String video = mVideoList.get(position);
            holder.bindData(video);
        }
    
        public class VideoViewHolder extends RecyclerView.ViewHolder {
            ManualPlayer userPlayer;
            VideoPlayerView playerView;
            public VideoViewHolder(View itemView) {
                super(itemView);
                //初始化控件
                playerView = (VideoPlayerView) itemView.findViewById(R.id.item_exo_player_view);
                userPlayer = new ManualPlayer((Activity) mContext, playerView);
            }
    
           /**
           *绑定数据源
           ***/
            public void bindData(String videoBean) {
                userPlayer.setTitle("" + getAdapterPosition());
                userPlayer.setPlayUri(videoBean);
                Glide.with(mContext)
                .load("http://i3.letvimg.com/lc08_yunzhuanma/201707/29/20/49/3280a525bef381311b374579f360e80a_v2_MTMxODYyNjMw/thumb/2_960_540.jpg")
                .into(playerView.getPreviewImage());
            }
        }
    

2.列表播放周期方法 列表在Activity或者Fragment 实现相应周期方法

                  protected void onPause() {
                      super.onPause();
                      VideoPlayerManager.getInstance().onPause();
                  }
                  @Override
                   protected void onResume() {
                      super.onResume();
                      VideoPlayerManager.getInstance().onResume();
                  }
                  @Override
                  protected void onDestroy() {
                      super.onDestroy();
                      VideoPlayerManager.getInstance().onDestroy();
                  }
                  @Override
                  public void onBackPressed() {
                      //返回监听类
                      if (VideoPlayerManager.getInstance().onBackPressed()){
                          finish();
                      }
                  }

四.数据源工厂类

1.默认数据源

      缓存 : CacheDataSinkFactory,CacheDataSourceFactory
      http : DefaultDataSourceFactory,DefaultHttpDataSourceFactory
      Priority : PriorityDataSourceFactory

2 自定义数据源引用

  compile 'com.google.android.exoplayer:extension-okhttp:r2.5.1'
  compile 'com.google.android.exoplayer:extension-rtmp:r2.5.1'

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

yjplay's People

Contributors

yangchaojiang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.