All Projects → Krupen → Autoplayvideos

Krupen / Autoplayvideos

Licence: apache-2.0
Android library to auto-play/pause videos from url in recyclerview.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Autoplayvideos

Animatedrecyclerview
RecyclerView with layout animations
Stars: ✭ 220 (-77.57%)
Mutual labels:  library, recyclerview
Google Books Android Viewer
Android library to bridge between RecyclerView and sources like web page or database. Includes demonstrator (Google Books viewer)
Stars: ✭ 37 (-96.23%)
Mutual labels:  library, recyclerview
Spannedgridlayoutmanager
Android RecyclerView.LayoutManager that resizes and reorders views based on SpanSize
Stars: ✭ 492 (-49.85%)
Mutual labels:  library, recyclerview
Omegarecyclerview
Custom RecyclerView with additional functionality. Allow you add divider, itemSpace, emptyView, sticky header and some other features
Stars: ✭ 132 (-86.54%)
Mutual labels:  library, recyclerview
Candyview
Implement any RecyclerView in just 1 Line. CandyView handles everything for you.
Stars: ✭ 15 (-98.47%)
Mutual labels:  library, recyclerview
Simple Sh Datascience
A collection of Bash scripts and Dockerfiles to install data science Tool, Lib and application
Stars: ✭ 32 (-96.74%)
Mutual labels:  library
Lib9wada
Wonderful library with lots of useful functions, algorithms and data structures in C, link it with -l9wada
Stars: ✭ 35 (-96.43%)
Mutual labels:  library
Restless
Express.js api, type safe validations and more
Stars: ✭ 32 (-96.74%)
Mutual labels:  library
Mod Pbxproj
A python module to manipulate XCode projects
Stars: ✭ 959 (-2.24%)
Mutual labels:  library
Hhcustomcorner
Awesome library to customize corners of UIView and UIButton. Now you can customize each corner differently
Stars: ✭ 36 (-96.33%)
Mutual labels:  library
Pony Crdt
Delta-State Convergent Replicated Data Types (ẟ-CRDTs) for the Pony language. 🐴 ↔️
Stars: ✭ 35 (-96.43%)
Mutual labels:  library
Substitution Schedule Parser
Java library for parsing schools' substitution schedules. Supports multiple different systems mainly used in the German-speaking countries, including Untis, svPlan, and DAVINCI
Stars: ✭ 33 (-96.64%)
Mutual labels:  library
Jenkins Library
Shared Library for Jenkine Pipeline
Stars: ✭ 32 (-96.74%)
Mutual labels:  library
Lwc
⚡️ LWC - A Blazing Fast, Enterprise-Grade Web Components Foundation
Stars: ✭ 974 (-0.71%)
Mutual labels:  library
Datasafe
Datasafe - flexible and secure data storage and document sharing using cryptographic message syntax for data encryption
Stars: ✭ 32 (-96.74%)
Mutual labels:  library
Unicode Bidirectional
A Javascript implementation of the Unicode 9.0.0 Bidirectional Algorithm
Stars: ✭ 35 (-96.43%)
Mutual labels:  library
Ethkit
Ethereum dev toolkit for Go
Stars: ✭ 32 (-96.74%)
Mutual labels:  library
Smartisandialog
Smartisan style Dialog.
Stars: ✭ 33 (-96.64%)
Mutual labels:  library
Angular Library Starter Kit
Angular 5 Library Starter Kit based on Angular-CLI
Stars: ✭ 35 (-96.43%)
Mutual labels:  library
Rhashmap
Robin Hood hash map library
Stars: ✭ 33 (-96.64%)
Mutual labels:  library

AutoplayVideos

API Android Arsenal Android Weekly AndroidDev Digest

Show some ❤️ and star the repo to support the project

GitHub stars GitHub forks GitHub watchers GitHub followers
Twitter Follow

This library is created with the purpose to implement recyclerview with videos easily.

It is targeted at solving following problems:

  1. Flicker when scrolling.
  2. Lag or skipping frames when video starts.
  3. OutOfMemory errors.

And it has following features:

  1. Auto-play videos when in view.
  2. Auto-pause videos when not in view or partially in view.
  3. Mute/Un-mute videos.
  4. Option to play only first visible video.
  5. Download videos to local storage in background for faster loading.

Demo

autoplayvideos_demo

Download

Gradle

Step 1. Add the jCenter repository to your project-level build.gradle file

allprojects {
	repositories {
		jcenter()
	}
}

Step 2. Add the dependency to your app-level build.gradle file:

dependencies {
	 compile 'com.allattentionhere:autoplayvideos:0.2.0'
}

Or Maven

<dependency>
  <groupId>com.allattentionhere</groupId>
  <artifactId>autoplayvideos</artifactId>
  <version>0.2.0</version>
  <type>pom</type>
</dependency>

Permissions

Add below permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Usage

Add AAH_VideoImage to your xml file for single list item single_card.xml:

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       
       <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

             <FrameLayout
                android:layout_width="300dp"
                android:layout_height="150dp">

                <com.allattentionhere.autoplayvideos.AAH_VideoImage
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <ImageView
                    android:id="@+id/img_vol"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|bottom"
                    android:layout_margin="8dp"
                    android:src="@drawable/ic_unmute"/>
            </FrameLayout>

            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center" />
		
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Add AAH_CustomRecyclerView to your Activity layout xml MainActivity.xml:

<?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="match_parent"
    android:orientation="vertical">

    <com.allattentionhere.autoplayvideos.AAH_CustomRecyclerView
        android:id="@+id/rv_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
	
</LinearLayout>

Set Adapter with following specifics:

  1. Adapter should extend AAH_VideosAdapter.
  2. ViewHolder should extend AAH_CustomViewHolder.
  3. Set thumbnail image url and video url in onBindViewHolder method.
public class MyVideosAdapter extends AAH_VideosAdapter {

    private List<MyModel> list;
    Picasso picasso;

    public class MyViewHolder extends AAH_CustomViewHolder {
        final TextView tv;
	final ImageView img_vol,img_playback;
        boolean isMuted; //to mute/un-mute video (optional)
	
        public MyViewHolder(View x) {
            super(x);
            tv = ButterKnife.findById(x, R.id.tv);
	    img_vol = ButterKnife.findById(x, R.id.img_vol);
	    img_playback = ButterKnife.findById(x, R.id.img_playback);
        }
    }

    public MyVideosAdapter(List<MyModel> list_urls, Picasso p) {
        this.list = list_urls;
        this.picasso = p;
    }

    @Override
    public AAH_CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_card, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(AAH_CustomViewHolder holder, int position) {
        ((MyViewHolder) holder).tv.setText(list.get(position).getName());

        //todo
        holder.setImageUrl(list.get(position).getImage_url());
        holder.setVideoUrl(list.get(position).getVideo_url());
        //load image/thumbnail into imageview
        if (list.get(position).getImage_url() != null && !list.get(position).getImage_url().isEmpty())
            picasso.load(holder.getImageUrl()).config(Bitmap.Config.RGB_565).into(holder.getAAH_ImageView());
    }
    
    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}

Finally setActivity in your Activity before setting the adapter and (Optional) scroll programmatically to initiate videos on initial screen:

    recyclerView.setActivity(this); //todo before setAdapter
    recyclerView.setAdapter(mAdapter);

Play videos initially (Optional)

Call these two functions to start video playback when the screen launches and user hasn't scrolled.

recyclerView.smoothScrollBy(0,1);
recyclerView.smoothScrollBy(0,-1);

Play only 1st video (Optional)

Setting this parameter will play video only in 1st completely visible RecyclerView ViewHolder.

recyclerView.setPlayOnlyFirstVideo(true); // false by default

Download videos to local storage (Optional)

You can start downloading video in background on viewholder loaded. You can change download path.

recyclerView.setDownloadPath(Environment.getExternalStorageDirectory() + "/MyVideo"); //optional
recyclerView.setDownloadVideos(true); // false by default

Optionally you can start pre-downloading all the videos by passing list of URLs to function as below:

List<String> urls = new ArrayList<>();
 for (MyModel object : modelList) {
     if (object.getVideo_url() != null && object.getVideo_url().endsWith(".mp4"))
         urls.add(object.getVideo_url());
 }
recyclerView.preDownload(urls);

Add below permission to AndroidManifest.xml. Ask for runtime permission in devices on Marshmallow and above.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Remove check for .mp4 (Optional)

By default it checks for url to end with .mp4 else it is not considered as video URL. You can override this by setting parameter as below. Please use this with caution and make sure you provide video URL only.

recyclerView.setCheckForMp4(false); // true by default

Pause videos manually (Optional)

Call the following method to stop the videos when Activity/Fragment stops (User receives call, minimizes app etc)

    @Override
    protected void onStop() {
        super.onStop();
        recyclerView.stopVideos();
    }

Resume videos when app resumes (Optional)

Call the following method to resume videos when App resumes (opening app after minimizing it, etc)

    @Override
    protected void onResume() {
        super.onResume();
        recyclerView.playAvailableVideos(0);
    }

Play partially visible video (Optional)

Call the following method to set the percentage of view that needs to be visible for video to start playing.

    recyclerView.setVisiblePercent(50);

Get callbacks when videos starts and pauses

You can override the below methods of AAH_CustomViewHolder to get callback when video starts to play or pauses.

	@Override
        public void videoStarted() {
            super.videoStarted();
            img_playback.setImageResource(R.drawable.ic_pause);
            if (isMuted) {
                muteVideo();
                img_vol.setImageResource(R.drawable.ic_mute);
            } else {
                unmuteVideo();
                img_vol.setImageResource(R.drawable.ic_unmute);
            }
        }
        @Override
        public void pauseVideo() {
            super.pauseVideo();
            img_playback.setImageResource(R.drawable.ic_play);
        }

Play or pause videos manually

You can allow the user to play or pause any video by adding below code in onBindViewHolder:

	((MyViewHolder) holder).img_playback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.isPlaying()) {
                    holder.pauseVideo();
                    holder.setPaused(true);
                } else {
                    holder.playVideo();
                    holder.setPaused(false);
                }
            }
        });

Mute or Unmute the videos

Video can be muted/unmuted by adding below code in onBindViewHolder:

	holder.getAah_vi().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((MyViewHolder) holder).isMuted) {
                    holder.unmuteVideo();
                    ((MyViewHolder) holder).img_vol.setImageResource(R.drawable.ic_unmute);
                } else {
                    holder.muteVideo();
                    ((MyViewHolder) holder).img_vol.setImageResource(R.drawable.ic_mute);
                }
                ((MyViewHolder) holder).isMuted = !((MyViewHolder) holder).isMuted;
            }
        });

Set looping on videos

Set looping on videos by adding below code in onBindViewHolder:

holder.setLooping(true); //optional - true by default

Cache Videos (Optional)

It is recommended to setDownloadVideos(value) to true. However if you do not want to use this option, we recommend you to use AndroidVideoCache library. Here is how you can use it along with our library:

Initilize HttpProxyCacheServer

Initilize HttpProxyCacheServer in your Application class and register Application class in your Manifest file.

public class MyApplication extends Application {
	private static HttpProxyCacheServer proxy;

	public static HttpProxyCacheServer getProxy() {
	    return proxy;
	}

	@Override
	public void onCreate() {
	    super.onCreate();
	    proxy = new HttpProxyCacheServer(this);
	}
}	

Set proxy video URL

In your VideosAdapter, set the Video URL as follows:

holder.setVideoUrl(MyApplication.getProxy().getProxyUrl(list.get(position).getVideo_url()+"")); // url should not be null

You can also use advanced options for caching that the library supports. Refer to their documentation for the same.

Use Cloudinary (Optional)

It is recommended to use Cloudinary.com to host your videos as it provides easy thumbnail-generation and resizing/cropping videos on-the-fly.

Changelog

Our other libraries

Apps by developer

Price Stalker Show Card Game Safio chat

License

Copyright 2017 Krupen Ghetiya

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].