2

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;
}
}
Rohit B
  • 165
  • 2
  • 26

1 Answers1

0

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.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • Sir, i want to play youtube video using Firebase database and dont want to add URL in code, i want to add URL to database and then get it from there – Rohit B Aug 05 '17 at 20:27
  • It is not the URL but only the videoID. if you are planning to play the youtube video using url, try checking this https://stackoverflow.com/a/20193658/5995040. – Mr.Rebot Aug 05 '17 at 20:35
  • The suggestion I provided is to save the videoID in the database then get the videoID as an object from Firebase database and load it using Youtube Android Player API. – Mr.Rebot Aug 05 '17 at 20:37
  • Sir but what if i want to add new video to database, i cant add it from code evertime, from where i will add new video id sir once the application is installed – Rohit B Aug 06 '17 at 18:42
  • Since you are pulling from the database, this will make your youtubeID or URL a variable – Mr.Rebot Aug 06 '17 at 18:44
  • Kindly read the docs like [Read and Write Data on Android](https://firebase.google.com/docs/database/android/read-and-write) and the [YouTube Android Player API](https://developers.google.com/youtube/android/player/) – Mr.Rebot Aug 06 '17 at 18:48
  • 1
    Thank you so much sir, it worked for me :) you are awsome – Rohit B Aug 09 '17 at 09:35