Project 8: Build a Video Sharing App, Fetch Video to Firebase

   Firebase Cloud Storage is an excellent cloud service built for app developers who need to store and serve content, such as photos or videos and other files. Once uploaded, these files can be accessed using an SDK and delivered on a website or an app.

Over the past few years, Firebase cloud storage has dramatically simplified app development by providing reliable, secure, and scalable object storage in the cloud.

Video Streaming with Firebase


Firebase is great at storing files and serving them as it is.

One can also deliver videos directly from Firebase storage to the user's device. It will rely on the Range Requests from the client's device and load partial content.

While this is not "streaming" the way youtube or Netflix stream their videos (using adaptive bitrate streaming techniques), it still does qualify as video streaming, given its definition.

step 1:-

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragements.HomeFragement">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/homeRecycler"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/homeProgressBar"
android:layout_gravity="center"/>
</FrameLayout>

step 2:-

video_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="?attr/selectableItemBackground">

<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#b6dad9d9"
android:minHeight="180dp" />

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imgProfile"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/imgThumb"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/ic_account" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgThumb"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@id/imgProfile"
android:orientation="vertical">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="2"
android:padding="3dp"
android:text="Video Title"
android:textColor="@color/black"
android:textSize="16dp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp">

<TextView
android:id="@+id/txtChannelName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Channel name"
android:textSize="12dp" />

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="View"
android:textSize="12dp" />

<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Date"
android:textSize="12dp" />


</LinearLayout>

</LinearLayout>
</RelativeLayout>

step 3:-

HomeFragment.java

package com.sandhya.youtube.fragements;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.sandhya.youtube.R;
import com.sandhya.youtube.adapter.ContentAdapter;
import com.sandhya.youtube.models.ContentModels;

import java.util.ArrayList;
import java.util.Collections;

public class HomeFragement extends Fragment {

public HomeFragement(){

}
RecyclerView homeRecycler;
ArrayList<ContentModels> models;
ContentAdapter contentAdapter;
DatabaseReference reference;
ProgressBar homeProgressBar;
@SuppressLint("MissingInflatedId")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home_fragement, container, false);

homeRecycler = view.findViewById(R.id.homeRecycler);
homeRecycler.setHasFixedSize(true);
homeProgressBar = view.findViewById(R.id.homeProgressBar);
reference = FirebaseDatabase.getInstance().getReference().child("Contents");
getAllVideo();

return view;
}
public void getAllVideo(){

models = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
homeProgressBar.setVisibility(View.GONE);
models.clear();
for ( DataSnapshot dataSnapshot : snapshot.getChildren()){
ContentModels model = dataSnapshot.getValue(ContentModels.class);
models.add(model);
}
//shu
Collections.shuffle(models);
contentAdapter = new ContentAdapter(getActivity(),models);
homeRecycler.setAdapter(contentAdapter);
contentAdapter.notifyDataSetChanged();
}else {
homeProgressBar.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), "No data found", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getActivity(), ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}

step 4:-

ContentModels.java

package com.sandhya.youtube.models;

public class ContentModels {
String video_title,channel_name,views,date,id,type,publisher,description,video_url,video_tags,video;

public ContentModels() {
}

public ContentModels(String video_title, String channel_name, String views, String date, String id, String type, String publisher, String description, String video_url, String video_tags, String video) {
this.video_title = video_title;
this.channel_name = channel_name;
this.views = views;
this.date = date;
this.id = id;
this.type = type;
this.publisher = publisher;
this.description = description;
this.video_url = video_url;
this.video_tags = video_tags;
this.video = video;
}

public String getVideo_title() {
return video_title;
}

public void setVideo_title(String video_title) {
this.video_title = video_title;
}

public String getChannel_name() {
return channel_name;
}

public void setChannel_name(String channel_name) {
this.channel_name = channel_name;
}

public String getViews() {
return views;
}

public void setViews(String views) {
this.views = views;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getVideo_url() {
return video_url;
}

public void setVideo_url(String video_url) {
this.video_url = video_url;
}

public String getVideo_tags() {
return video_tags;
}

public void setVideo_tags(String video_tags) {
this.video_tags = video_tags;
}

public String getVideo() {
return video;
}

public void setVideo(String video) {
this.video = video;
}
}

step 5:-

ContentAdapter.java

package com.sandhya.youtube.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.sandhya.youtube.R;
import com.sandhya.youtube.models.ContentModels;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
Context context;
ArrayList<ContentModels> models;

DatabaseReference reference;
DatabaseReference databaseReference;
FirebaseUser user;

public ContentAdapter(Context context, ArrayList<ContentModels> models) {
this.context = context;
this.models = models;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.video_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

ContentModels model = models.get(position);
if (model != null) {
Glide.with(context).load(model.getVideo_url()).into(holder.imgThumb);
// Glide.with(context).load(model.getPublisher()).into(holder.imgProfile);
// holder.txtChannelName.setText(model.getChannel_name());
holder.txtView.setText(model.getViews());
holder.txtDate.setText(model.getDate());
holder.txtTitle.setText(model.getVideo_title());
setDate(model.getPublisher(),holder.imgProfile,holder.txtChannelName);
}
}

private void setDate(String publisher, CircleImageView imgProfile, TextView txtChannelName) {
reference = FirebaseDatabase.getInstance().getReference().child("Channels");
reference.child(publisher).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
String channelName = snapshot.child("Channel Name").getValue().toString();
String logo = snapshot.child("Profile_Picture").getValue().toString();
txtChannelName.setText(channelName);
Glide.with(context).load(logo).placeholder(R.drawable.ic_account).into(imgProfile);

}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {

}
});
}

@Override
public int getItemCount() {
return models.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgThumb;
CircleImageView imgProfile;
TextView txtTitle, txtChannelName, txtView, txtDate;

public ViewHolder(@NonNull View itemView) {
super(itemView);

imgThumb = itemView.findViewById(R.id.imgThumb);
imgProfile = itemView.findViewById(R.id.imgProfile);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtChannelName = itemView.findViewById(R.id.txtChannelName);
txtView = itemView.findViewById(R.id.txtView);
txtDate = itemView.findViewById(R.id.txtDate);
}
}
}


output:-








Post a Comment

Post a Comment (0)

Previous Post Next Post