How to add(Part-2) Video in FirebaseRecyclerAdapter in android studio
First read Add(Part_1) in FirebaseRecyclerAdapter in android studio
Let's start the second part
Add below dependency in app-level build Gradle
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.phoneauthentication"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:26.1.0')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.github.joielechong:countrycodepicker:2.4.2'
implementation 'com.karumi:dexter:6.2.2'
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.firebaseui:firebase-ui-database:6.2.1'
implementation 'com.google.android.exoplayer:exoplayer:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-core:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.7.3'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
↓
Add below code in project-level Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.google.gms:google-services:4.3.10'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
↓
Add below code AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.firstproject">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.firstproject">
//Below activity name differ , as you type activity name during creating
//Below activity code is automatic generated
<activity android:name=".video_Player"/>
<activity android:name=".videos" />
<activity android:name=".dashboardactivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
↓
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.google.gms:google-services:4.3.10'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
↓
Add below code AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.firstproject">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.firstproject">
//Below activity name differ , as you type activity name during creating
//Below activity code is automatic generated
<activity android:name=".video_Player"/>
<activity android:name=".videos" />
<activity android:name=".dashboardactivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
↓
Add below code in string.xml
<resources>
<string name="app_name">phone authentication</string>
<string name="_1234567890">1234567890</string>
<string name="enter_your_valid_mobile_no">enter your valid mobile no</string>
<string name="Send_Otp">send otp</string>
<string name="Enter_otp">enter otp</string>
<string name="Submit">submit</string>
<string name="ReSend_Otp">resend otp</string>
<string name="Phone_verification">phone varification</string>
<string name="add_song">add song</string>
<string name="enter_video_name">Enter video name</string>
<string name="browse_video">Browse video</string>
<string name="upload_video">Upload video</string>
<string name="video_name">Video name</string>
<string name="todo">todo</string>
<string name="song_name">song_name</string>
</resources>
↓
Create an android project
↓
Create empty activity, click on Empty activity
↓
How to create an android project in android studio
Right-click on Layout and click on Layout Resource file
↓
Type name singlerow.xml and click ok
↓
Type below code in singlerow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/fl_ad_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="203dp"
android:background="@color/white">
<VideoView
android:id="@+id/exoplyerview"
android:layout_width="match_parent"
android:layout_height="172dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="31dp"
android:contentDescription="@string/todo" />
<ImageButton
android:id="@+id/video_thumbnail"
android:layout_width="match_parent"
android:layout_height="177dp"
android:contentDescription="@string/todo" />
<TextView
android:id="@+id/video_name"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_below="@id/video_thumbnail"
android:layout_marginStart="5dp"
android:text="@string/video_name"
android:textSize="15sp" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
↓
Create activity click on Empty activity and type name video
Add below code in video.java(Exp: activity.java)
package com.example.phoneauthentication;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.HashMap;
import java.util.Random;
public class videos extends AppCompatActivity {
private static final int item_data = 0;
private static final int item_banner = 1;
RecyclerView recyclerView;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
recyclerView = findViewById(R.id.recyclerview);
progressBar = findViewById(R.id.mprogressbar);//set layout in recyclerview
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Type same name Videos name ,when type Videos name in How to add Video(Part-1) in FirebaseRecyclerAdapter in android studio
FirebaseRecyclerOptions<songs> options = new FirebaseRecyclerOptions.Builder<songs>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Videos"), songs.class)
.build();How to create a new java class in android studio
FirebaseRecyclerAdapter<songs, VideoViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<songs, VideoViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull VideoViewHolder holder, int position, @NonNull songs model) {//getVideoname,getVideourl belongs to songs class ↓holder.video_name.setText(model.getVideoname().toUpperCase());
String file = model.getVideourl();//postkey position of song
String postkey = getRef(position).getKey();//postkey used ,when postkey is not equal to null
assert postkey != null;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();//user used ,when postkey is not equal to null,user is object of FirebaseUserassert user != null;
String currentuser = user.getUid();//Glide used for show thumbnail
Glide.with(getApplicationContext()).asBitmap().load(model.getVideourl()).into((holder).video_thumb);
holder.video_thumb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//putExtra used for send current user,postkey,song type,pathfile etc send to other activity
String type=model.getSotype();
Intent intent = new Intent(videos.this, video_Player.class);
intent.putExtra("path file", file);
intent.putExtra("song name", model.getVideoname());
intent.putExtra("song type",type);
intent.putExtra("postkey",postkey);
intent.putExtra("current user",currentuser);
startActivity(intent);
}
});
holder.video_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//putExtra used for send current user,postkey,song type,pathfile etc send to other activityString type=model.getSotype();
Intent intent = new Intent(videos.this, video_Player.class);
intent.putExtra("path file", file);
intent.putExtra("song name", model.getVideoname());
intent.putExtra("song type",type);
intent.putExtra("postkey",postkey);
intent.putExtra("current user",currentuser);
startActivity(intent);
}
});
}
@NonNull
@Override
public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//LayoutInFlater user for set singlrow.xml layout in Recyclerview
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow, parent, false);
return new VideoViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
recyclerView.setAdapter(firebaseRecyclerAdapter);
}@Override
public boolean onCreateOptionsMenu(Menu menu) {//getMenuInflater used for bind custom menu}
getMenuInflater().inflate(R.menu.custom_menu, menu);
MenuItem mItem = menu.findItem(R.id.add_videos);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId()==R.id.add_videos){
try {
pickvideo();
} catch (Exception e) {
e.printStackTrace();
}
}
return super.onOptionsItemSelected(item);
}
private void pickvideo() {
startActivity(new Intent(videos.this,addvideo.class));
}
↓Create VideoViewHolder class ,Type below code in VideoViewHolder.java
package com.example.phoneauthentication;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class VideoViewHolder extends RecyclerView.ViewHolder {
VideoView exoPlayerview;
TextView video_name;
ImageButton video_thumb;
public VideoViewHolder(@NonNull View itemView) {
super(itemView);
exoPlayerview = itemView.findViewById(R.id.exoplyerview);
video_name = itemView.findViewById(R.id.video_name);
video_thumb = itemView.findViewById(R.id.video_thumbnail);
}
}↓
Create java class type name songs.java
and type below code in songs.javapackage com.example.phoneauthentication;
public class songs {//Type videoname,videourl type same according to How to add Video(Part-1) in FirebaseRecyclerAdapter in android studio
String videoname,videourl,sotype;
public songs(String videoname, String videourl) {
this.videoname = videoname;
this.videourl = videourl;
}
public String getVideoname() {
return videoname;
}
public String getVideourl() {
return videourl;
}
songs(){}
public String getSotype() {
return sotype;
}
}↓Create custom_menu.xml ,
right on click on Menu and type on Menu resource file↓Lets run app
Comments
Post a Comment