具有cardview机制的YouTube视频播放器无法停止反应

时间:2016-12-30 09:10:07

标签: android youtube-api android-cardview

我曾尝试建立一个项目,其中来自YouTube的6个不同视频将以cardview风格提供。然后选中的将播放。但由于某些错误或错误,它正在崩溃。

错误显示在目标代码的最后一部分的下面给出的代码中。

package com.chandra.user.newstrailerapp.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;

/**
 * Created by USER on 11-11-2016.
 */

public class YoutubecardAdapter extends     
RecyclerView.Adapter<YoutubecardAdapter.VideoInfoHolder> {

    //VIDEO IDs

    private String[] VideoId = {"AbzdfUNsSsM",
            "6QgLkw-rfc4",
            "nubDFeiUAsI",
            "poLjq0u4_5A",
            "r4O4Xec60_k",
            "1Q8fG0TtVAY",
            "MQEFmHsseaU"};
    Context ctx;

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

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

@Override
public void onBindViewHolder(final VideoInfoHolder holder, final int position) {
    final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoaderListner = 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(ctx.getResources().getString(R.string.YOUTUBE_API_KEY), new YouTubeThumbnailView.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {

            youTubeThumbnailLoader.setVideo(VideoId[position]);
            youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoaderListner);
        }

        @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, ctx.getResources().getString(R.string.YOUTUBE_API_KEY), VideoId[getLayoutPosition()]);
        ctx.startActivity(intent);
    }

}

}

0 个答案:

没有答案