在Android上的同一个YouTubePlayerView上播放YouTube视频

时间:2016-05-17 10:17:40

标签: android youtube

我想在点击recyclerview的项目时在YouTubePlayerView上播放视频。第一个视频播放效果很好,但是当我点击了再循环视频的任何项目时,YouTubePlayerView都没有加载视频。

这是我的代码

package com.bidhee.nagariknews.views.activities;

import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bidhee.nagariknews.R;
import com.bidhee.nagariknews.Utils.StaticStorage;
import com.bidhee.nagariknews.controller.server_request.ServerConfig;
import com.bidhee.nagariknews.controller.server_request.WebService;
import com.bidhee.nagariknews.model.Multimedias;
import com.bidhee.nagariknews.widget.GalleryAdapter;
import com.bidhee.nagariknews.widget.RecyclerItemClickListener;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ronem on 2/26/16.
 */
public class YoutubePlayerActivity extends YouTubeBaseActivity implements RecyclerItemClickListener.OnItemClickListener {
    YouTubePlayer.OnInitializedListener listener;

    @Bind(R.id.view)
    YouTubePlayerView playerView;

    @Bind(R.id.video_title_text_view)
    TextView videoTitleTextView;
    @Bind(R.id.no_of_view_text_view)
    TextView noOfViewTextView;
    @Bind(R.id.no_of_like_text_view)
    TextView noOfLikeTextView;
    @Bind(R.id.no_of_dislike_text_view)
    TextView noOfDislikeTextView;

    @Bind(R.id.share_video)
    ImageView shareVideo;
    @Bind(R.id.video_recycler_view)
    RecyclerView videoRecyclerView;


    private ArrayList<Multimedias> multimedias;
    private GalleryAdapter galleryAdapter;
    private int position;
    Response.Listener<String> response;
    Response.ErrorListener errorListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.youtube_player_activity);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Dashboard.COLOR_PRIMARY_DARK);
        }

        multimedias = getIntent().getExtras().getParcelableArrayList(StaticStorage.KEY_VIDEO_BUNDLE);
        position = getIntent().getIntExtra("position", 0);

        handleSererResponse();
        playVideo(multimedias.get(position).getTitle(), multimedias.get(position).getMultimediaPath());

        /**
         * Related vieos
         */
        loadRelatedPlayList();
    }

    private void loadRelatedPlayList() {
        galleryAdapter = new GalleryAdapter(multimedias, StaticStorage.VIDEOS);

        videoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        videoRecyclerView.setHasFixedSize(true);
        videoRecyclerView.setItemAnimator(new DefaultItemAnimator());
        videoRecyclerView.setAdapter(galleryAdapter);
        videoRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(YoutubePlayerActivity.this, 0, this));
    }

    private void playVideo(final String title, final String videoId) {

        listener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                videoTitleTextView.setText(title);
                youTubePlayer.loadVideo(videoId);
                WebService.getServerData(ServerConfig.getVideoInfoUrl(videoId), response, errorListener);
            }

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

            }
        };

        playerView.initialize(getString(R.string.apikey), listener);
    }

    private void handleSererResponse() {

        response = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("response", response);
                try {
                    JSONObject nodeObject = new JSONObject(response);
                    JSONArray items = nodeObject.getJSONArray("items");
                    JSONObject subnode = items.getJSONObject(0);
                    JSONObject stats = subnode.getJSONObject("statistics");
                    String viewCOunt = NumberFormat.getNumberInstance(Locale.US).format(Integer.parseInt(stats.getString("viewCount")));
                    String likeCOunt = NumberFormat.getNumberInstance(Locale.US).format(Integer.parseInt(stats.getString("likeCount")));
                    String dislikeCount = NumberFormat.getNumberInstance(Locale.US).format(Integer.parseInt(stats.getString("dislikeCount")));

                    noOfViewTextView.setText(viewCOunt);
                    noOfLikeTextView.setText(likeCOunt);
                    noOfDislikeTextView.setText(dislikeCount);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        };
    }

    @Override
    public void onItemClick(View view, int parentPosition, int position) {

        playVideo(multimedias.get(position).getTitle(), multimedias.get(position).getMultimediaPath());
    }
}

1 个答案:

答案 0 :(得分:1)

  

我点击了未加载的recyclerview视频的任何项目   YouTubePlayerView。

不幸的是,这是预期的行为。您需要做的是跟踪当前NUMBER并在加载新版本之前将其释放。