gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:38:55 25 4
gpt4 key购买 nike

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

I am using Android Studio and Firebase

Firebase 回收器适配器类

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

getter 和 setter 的后期类(class)

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.

您可以按照 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) {

}
});

希望这对您有所帮助。

关于android - 在 Android 中使用 Firebase 在应用程序中添加 Youtube API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com