我正在尝试使用 YouTube API 创建一个包含多个 YouTube 视频的 RecyclerView。我试过 ExoPlayer 之前没有成功。ChromeWebView 有效,但这不是我想要的。应用程序运行,但 RecyclerView 中显然没有视频。你有修复它的建议吗?
我的主要活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<VideoPlayer> videoList = new ArrayList<>();
videoList.add(new VideoPlayer("f1uFsQ6MUD4"));
videoList.add(new VideoPlayer("AQCiGXamz6Q"));
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
adapter = new VideoAdapter(videoList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
适配器:
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
private ArrayList<VideoPlayer> videoPlayers;
public VideoAdapter(ArrayList<VideoPlayer> videoList) {
videoList = videoPlayers;
}
@NonNull
@Override
public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_video_player, parent, false);
VideoViewHolder vvh = new VideoViewHolder(view);
return vvh;
}
@Override
public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
VideoPlayer currentVideoPlayer = videoPlayers.get(position);
// holder.URL = ?
}
public static class VideoViewHolder extends RecyclerView.ViewHolder {
public String URL;
public YouTubePlayerView playerView;
public Button playButton;
public VideoViewHolder(@NonNull View itemView) {
super(itemView);
playButton = itemView.findViewById(R.id.playButton);
playerView = itemView.findViewById(R.id.youtubePlayerView);
//URL = ?
}
}
@Override
public int getItemCount() {
return 0; //videoPlayers.size()
}
视频播放器类:
public class VideoPlayer extends YouTubeBaseActivity {
YouTubePlayerView playerView;
Button playButton;
com.google.android.youtube.player.YouTubePlayer.OnInitializedListener onInitializedListener;
private String url;
public VideoPlayer(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
playButton = (Button) findViewById(R.id.playButton);
playerView = (YouTubePlayerView) findViewById(R.id.youtubePlayerView);
onInitializedListener = new com.google.android.youtube.player.YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(com.google.android.youtube.player.YouTubePlayer.Provider provider, com.google.android.youtube.player.YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(url);
}
@Override
public void onInitializationFailure(com.google.android.youtube.player.YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playerView.initialize(String.valueOf(APIKey.getKey()), onInitializedListener);
}
});
}