I am using Android Studio with Firebase as a database and want to implement Youtube API with it, what I want is that I enter the URL of video in Firebase Database and it should make the video available in my application. I have successfully achieved this with images and text but do not know how to make it work with video, please help. I have marked the line of code from where I am having problem in achieving this, it is highlighted as "THIS CODE" in below code. please ask me what you do not understand.
I am using Android Studio and Firebase
Firebase Recycler Adapter class
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setdescription(model.getDescription());
viewHolder.setimage(getApplicationContext(), model.getImage());
viewHolder.setsource(model.getSource());
viewHolder.setYoutube(getApplicationContext(),model.getYoutube());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.title_cardView);
post_title.setText(title);
}
public void setsource(String source){
TextView post_source = (TextView)mView.findViewById(R.id.source_cardView);
post_source.setText(source);
}
public void setdescription(String description){
TextView post_description = (TextView)mView.findViewById(R.id.description_cardView);
post_description.setText(description);
}
public void setYoutube(final Context context, final String youtube){
final YouTubePlayer youPlay = (YouTubePlayer)mView.findViewById(R.id.youtuber);
youPlay.with(context).loadVideo(youtube); // <----- THIS CODE
}
public void setimage(final Context ctx, final String image){
final ImageView post_image = (ImageView)mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(image).into(post_image);
}
});
}
}
post class for getters and setters
public class post {
private String title;
private String description;
private String image;
private String source;
private String youtube;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public post(){
}
public post(String title, String description, String image, String source,String youtube) {
this.title = title;
this.description = description;
this.image = image;
this.source = source;
this.youtube = youtube;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getYoutube() {
return youtube;
}
public void setYoutube(String youtube) {
this.youtube = youtube;
}
}
You can use the YouTube Android Player API
The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.
Using the API, you can load or cue videos into a player view embedded in your application's UI. You can then control playback programmatically. For example, you can play, pause, or seek to a specific point in the currently loaded video.
You can also register event listeners to get callbacks for certain events, such as the player loading a video or the player state changing. Finally, the API has helper functionality to support orientation changes as well as transitions to fullscreen playback.
You can follow this code implementation provided from a related SO post:
youTubePlayerView.initialize("YOUR API KEY",
new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("VIDEO_ID_HERE");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
Hope this helps.