如何在android中的RecyclerAdapter中加载youtuble视图?

时间:2016-04-11 07:16:12

标签: android youtube youtube-api android-recyclerview

我正在构建一个Android应用程序,我正在尝试在RecyclerAdapter上加载视频。

我做了什么:

我使用youtuble API加载缩略图和视频。我能够加载缩略图,如果我点击该缩略图,视频将在下一页全屏播放。

我需要什么:

我主要需要播放那个视频,而不是去下一个屏幕。

以下是RecyclerAdapter类代码

package com.recycler.youtube;

/**
* Created by admin-pc on 05-04-2016.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.VideoInfoHolder> {

//these ids are the unique id for each video
String[] VideoID = {"P3mAtvs5Elc", "nCgQDjiotG0", "P3mAtvs5Elc"};
Context ctx;
public  static  String key="AIzaSyD2XJ_kIpxiwIMxY_wlrjGXBl0AAG1DfjA";

public RecyclerAdapter(Context context) {
    this.ctx = context;
}

@Override
public VideoInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, parent, false);
    return new VideoInfoHolder(itemView);
}

@Override
public void onBindViewHolder(final VideoInfoHolder holder, final int position) {


    final YouTubeThumbnailLoader.OnThumbnailLoadedListener  onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener(){
        @Override
        public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

        }

        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
            youTubeThumbnailView.setVisibility(View.VISIBLE);
            holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
        }
    };

    holder.youTubeThumbnailView.initialize(key, new YouTubeThumbnailView.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
            youTubeThumbnailLoader.setVideo(VideoID[position]);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
        }

        @Override
        public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
            //write something for failure
        }
    });
}

@Override
public int getItemCount() {
    return VideoID.length;
}

public class VideoInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
    YouTubeThumbnailView youTubeThumbnailView;
    protected ImageView playButton;

    public VideoInfoHolder(View itemView) {
        super(itemView);
        playButton=(ImageView)itemView.findViewById(R.id.btnYoutube_player);
        playButton.setOnClickListener(this);
        relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
        youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
    }

    @Override
    public void onClick(View v) {
        Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) ctx, key, VideoID[getLayoutPosition()]);
        ctx.startActivity(intent);
        // Initializing video player with developer key
     //            youTubeView.initialize(key, this);
    }
 }

 }

我按照此Link来实施youtube api。

0 个答案:

没有答案