Youtube Thumbnail Loader在播放列表中加载第1个视频2次

时间:2018-04-11 05:01:06

标签: android youtube youtube-api youtube-data-api

我正在开发一个电子学习应用,我想在其中显示youtube播放列表。我有1个活动,其中我正在显示youtube播放器,并在其下方显示播放列表。我从我的服务器上获取YouTube上的thmbnails和他们的标题。在检索缩略图时,首先进行2次视频复制,并保留正确的剩余视频。

活动:

VideoList

public class VideoList extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener, YouTubeThumbnailLoader.OnThumbnailLoadedListener, YouTubePlayer.OnInitializedListener{


    YouTubePlayerFragment playerFragment;
    YouTubePlayer Player;
    YouTubeThumbnailView thumbnailView;
    YouTubeThumbnailLoader thumbnailLoader;
    RecyclerView VideoList;
    RecyclerView.Adapter adapter;
    List<Drawable> thumbnailViews;
    List<String> VideoId;
    String videoUrl;

    public List<VideoModel> videos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);

        thumbnailViews = new ArrayList<>();
        videos = new ArrayList<>();
        VideoList = (RecyclerView) findViewById(R.id.videolist);
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
        VideoList.setLayoutManager(layoutManager);
        adapter=new VideoListAdapter(videos);
        VideoList.setAdapter(adapter);
        VideoId = new ArrayList<>();
        thumbnailView = new YouTubeThumbnailView(this);
        thumbnailView.initialize("API-KEY", this);
        playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.VideoFragment);
        playerFragment.initialize("API-KEY", this);

    }



    @Override
    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
        getSub();
        thumbnailLoader = youTubeThumbnailLoader;
        youTubeThumbnailLoader.setOnThumbnailLoadedListener(VideoList.this);


    }

    @Override
    public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
        Toast.makeText(this, youTubeInitializationResult.toString(), Toast.LENGTH_LONG).show();
    }


    public void add() {
        adapter.notifyDataSetChanged();
        if (thumbnailLoader.hasNext())
            thumbnailLoader.next();
    }

    @Override
    public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
        thumbnailViews.add(youTubeThumbnailView.getDrawable());
        VideoId.add(s);
        add();
    }

    @Override
    public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
        Toast.makeText(this, errorReason.toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        Player=youTubePlayer;
        Player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
            @Override
            public void onFullscreen(boolean b) {
                VideoList.setVisibility(b?View.GONE:View.VISIBLE);
            }
        });
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

    }

    public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyView>{

        List<VideoModel> videos;

        public class MyView extends RecyclerView.ViewHolder{

            ImageView imageView;
            TextView textView;
            public MyView(View itemView) {
                super(itemView);
                imageView= (ImageView) itemView.findViewById(R.id.thumbnailView);
                textView = (TextView) itemView.findViewById(R.id.txtVideoTitle);
            }

        }

        public VideoListAdapter(List<VideoModel> videos){
            this.videos = videos;
        }

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

        @Override
        public void onBindViewHolder(VideoListAdapter.MyView holder, final int position) {
            holder.imageView.setImageDrawable(thumbnailViews.get(position));
            VideoModel videoModel = videos.get(position);
            if (videoModel.getVideo() != null) {
                holder.textView.setText(videoModel.getVideo());
            }
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Player.cueVideo(VideoId.get(position));
                }
            });
        }

        @Override
        public int getItemCount() {
            return thumbnailViews.size();
        }
    }

    public void getSub(){
        SimpleMultiPartRequest stringRequest = new SimpleMultiPartRequest(Request.Method.POST, VIDEO_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("result");
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                    videoUrl = jsonObject1.optString("VidLink");
                    thumbnailLoader.setPlaylist(videoUrl);
                    Player.cuePlaylist(videoUrl );
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject j = jsonArray.getJSONObject(i);

                        VideoModel videoModel = new VideoModel();

                        String video = j.optString("VidSubTopic");

                        videoModel.setVideo(video);

                        videos.add(videoModel);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setShouldCache(false);
        stringRequest.addStringParam("standard", standard);
        stringRequest.addStringParam("subject", subjectcode);
        stringRequest.addStringParam("chapter", topic);
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);
    }

}

0 个答案:

没有答案