2016-05-17 14 views
0

recyclerviewのアイテムをクリックすると、YouTubePlayerViewでビデオを再生したいと思います。最初のビデオはうまくいきますが、私がrecyclerviewビデオのアイテムをクリックしたときには、YouTubePlayerViewにロードされていません。androidの同じYouTubePlayerViewでyoutubeビデオを再生

は、ここに私のコード

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

私は YouTubePlayerViewにロードされていないrecyclerviewビデオの任意の項目をクリックします。

残念ながら、これは予想された動作です。あなたがしなければならないことは、現在のYouTubePlayerを追跡し、新しいものをロードする前にリリースすることです。

+0

と私はどのようにコードの一部でそれを達成することができますか? –

+0

'onInitializationSuccess'が呼び出されたときに' youTubePlayer'をメンバ値に格納します。 'onItemClick'がメンバ変数が'!= null'であるかどうかをチェックし、この場合relase()を呼び出すと – Blackbelt

+0

私はrelease()メソッドを見つけることができません:( –

関連する問題