项目作者: pankaj89

项目描述 :
MasterExoPlayer is lightweight utility for playing video inside RecyclerView.
高级语言: Kotlin
项目地址: git://github.com/pankaj89/MasterExoPlayer.git
创建时间: 2019-10-12T16:36:09Z
项目社区:https://github.com/pankaj89/MasterExoPlayer

开源协议:

下载


alt text

BETA VERSION

MasterExoPlayer for Recyclerview (build with kotlin)

#3 Line code for playing video inside RecyclerView

N|Solid

MasterExoPlayer is lightweight utility for helping Play Video inside RecyclerView.

Features

  • Easy to use (Just 3 line of code)
  • No Need to create different view holder to support playing video
  • Support for playing video inside horizontal recyclerview inside RecyclerView Item like instagram
  • Can handle autoplay, mute, logic to play by area(whether video is 75% visible then starts Play)
  • Just attach MasterExoPlayerHelper to recyclerview that’s enought, player will play most visible video automatically based on your configuration

Setup

Include the following dependency in your build.gradle files.

  1. // Project level build.gradle
  2. allprojects {
  3. repositories {
  4. ...
  5. maven { url 'https://jitpack.io' }
  6. }
  7. }
  8. // App level build.gradle
  9. dependencies {
  10. implementation 'com.github.pankaj89:MasterExoPlayer:1.4.5'
  11. }

Whats New

  • Added method to return playerview from MasterExoPlayerHelper, now we can customize player using getPlayer() on MasterExoPlayerHelper
    Example:
    masterExoPlayerHelper.getPlayerView().resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM

How to use

Attach to RecyclerView

1. Add MasterExoPlayer inside RecyclerView Item

  1. <com.master.exoplayer.MasterExoPlayer
  2. android:id="@+id/masterExoPlayer"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" ></com.master.exoplayer.MasterExoPlayer>

2. Set url of your video view to MasterExoPlayer inside your RecyclerView Adapter onBindViewHolder

  1. binding.frame.url = model.sources

3. Attach MasterExoPlayerHelper to RecyclerView

  1. val recyclerView: RecyclerView = ....
  2. val masterExoPlayerHelper = MasterExoPlayerHelper(mContext = this, id = R.id.masterExoPlayer)
  3. masterExoPlayerHelper.makeLifeCycleAware(this)
  4. masterExoPlayerHelper.attachToRecyclerView(recyclerView)
  5. //Used to customize attributes
  6. masterExoPlayerHelper.getPlayerView().apply {
  7. resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
  8. }

Configuration

Constructor parameters for MasterExoPlayerHelper

  1. 1. id : Int
  2. View id of ExoPlayerView used in item layout
  1. 2. autoPlay : Boolean
  2. If you want to autoplay video once loaded
  1. 3. playStrategy : Float
  2. Visible area from 0 to 1, Which matches to play video, default value = PlayStrategy.DEFAULT i.e 0.75 means 75% area visible to starts play
  1. 4. muteStrategy : Values from MuteStratagy.ALL or MuteStratagy.INDIVIDUAL
  2. Defines whether mute/unmute affects all rows or individual
  1. 5. defaultMute : Boolean
  2. If default video should be muted or not
  1. 6. loop:Int
  2. Defines if you want to loop the video, default is unlimited, if set to 1 it will play only 1 time then stoop.
  1. 7. useController : Boolean
  2. Defines if you want use controller for exo player or not. if set true then controller will be visible else hide, default will be false.
  1. 8. thumbHideDelay : Long
  2. Defines duration in millisecond, defines delay before hiding thumbnail image while video plays.

Listen for buffering or not

  1. //Inside onBindViewHolder of your RecyclerViewAdapter
  2. binding.masterExoPlayer.listener = object : ExoPlayerHelper.Listener {
  3. //Listen for buffering listener
  4. override fun onBuffering(isBuffering: Boolean) {
  5. super.onBuffering(isBuffering)
  6. Log.i("TAG", isBuffering.toString())
  7. }
  8. //Update mute/unmute icon on player ready callback.
  9. override fun onPlayerReady() {
  10. super.onPlayerReady()
  11. binding.ivVolume.visibility = View.VISIBLE
  12. if (binding.frame.isMute) {
  13. binding.ivVolume.setImageResource(R.drawable.ic_volume_off)
  14. } else {
  15. binding.ivVolume.setImageResource(R.drawable.ic_volume_on)
  16. }
  17. }
  18. override fun onStop() {
  19. super.onStop()
  20. binding.ivVolume.visibility = View.GONE
  21. }
  22. }

Special Thanks to

My Other Libraries

License

  1. Copyright 2017 Pankaj Sharma
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.