在 Android 中使用 Firebase 在应用程序中添加 Youtube API

Adding Youtube API in application using Firebase in Android

我正在使用带有 Firebase 的 Android Studio 作为数据库,并希望使用 Youtube API它,我想要的是我在 Firebase 数据库 中输入视频的 URL 它应该使视频在我的应用程序中可用.我已经成功地用图片和文字实现了这一点,但不知道如何让它与视频一起使用,请帮忙。我已经标记了我在实现这一点时遇到问题的代码行,它在下面的代码中突出显示为 "THIS CODE"。有什么不明白的请问我

I am using Android Studio and Firebase

Firebase 回收器适配器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 对于 getter 和 setter

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;
}
}

您可以使用 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.

您可以按照 :

提供的代码实现
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) {

                }
    });

希望对您有所帮助。