Youtube Video Is Not Stopped When Clicked In Horizontallistview Item
I am using Youtube Data API to Access the Video.I am successful on Displaying the thumbnail image on the ListView.When i start Application,and Click the one of List Item, it Loads
Solution 1:
you are initializing the player everytime your list item is clicked. It should be done only once and you don't have to use youTubePlayer.play(); following line will work youTubePlayer.cueVideo(video_Id);
Solution 2:
publicclassFragmentVideoDestinationextendsFragment {
privateFragmentActivity myContext;
YouTubePlayer YPlayer;
privatestatic final StringYoutubeDeveloperKey = "AIzaSyCLk_SWbZseee8lpSEb6rLTSvhL8YSLjEE";
StringNavigation_URL_Popular_Destination_video = "http://192.168.100.7:1337/api/popular_destinations";
HorizontalListView horizontalListView;
FrameLayout framelayoutvideo;
String video_Id;
String thumbnail_image;
String[] youtube_video_url;
String parsed_url;
ArrayList<ClassDestinationFragmentYoutubeVideo> destination_list_grid_vieo = newArrayList<>();
@Nullable@OverridepublicViewonCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.destination_grid_detail_video, container, false);
horizontalListView = (HorizontalListView) view.findViewById(R.id.horizontal_scroll_list_item);
framelayoutvideo = (FrameLayout) view.findViewById(R.id.youtubeFragment);
final YouTubePlayerSupportFragment youTubePlayerFragment = newYouTubePlayerSupportFragment();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.relative_layout_destination_video, youTubePlayerFragment);
fragmentTransaction.commit();
makeJsonPopularDestinationDetailPageVideo();
horizontalListView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
video_Id = destination_list_grid_vieo.get(position).getUrl();
System.out.println("position" + destination_list_grid_vieo.get(position).getUrl());
if (YPlayer != null) {
YPlayer.cueVideo(video_Id); // youTubePlayer.play();
}
}
});
youTubePlayerFragment.initialize(YoutubeDeveloperKey, newOnInitializedListener() {
@OverridepublicvoidonInitializationSuccess(Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer= youTubePlayer;
if(video_Id!=null)
YPlayer.cueVideo(video_Id);
}
}
}
@OverridepublicvoidonInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
}
});
return view;
}
@OverridepublicvoidonDestroy() {
super.onDestroy();
if (YPlayer!= null) {
YPlayer.release();
YPlayer = null;
}
}
privatevoidmakeJsonPopularDestinationDetailPageVideo() {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringURL1 = Navigation_URL_Popular_Destination_video + "/1";
StringRequest stringRequest = newStringRequest(Request.Method.GET, URL1,
newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String response) {
try {
//JSONObject jsonObject = newJSONObject(response);
JSONArray jArray = newJSONArray();
jArray = jsonObject.getJSONArray("videos");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObjectinner = jArray.getJSONObject(i);
thumbnail_image = jsonObjectinner.getString("thumbnail");
// youtube_video_url = jsonObjectinner.getString("url").substring(32, 43);
youtube_video_url = String.valueOf(jsonObjectinner.getString("url")).split("=");
parsed_url = youtube_video_url[1];
destination_list_grid_vieo.add(newClassDestinationFragmentYoutubeVideo(thumbnail_image, parsed_url));
System.out.println("Destination list" + destination_list_grid_vieo.get(i).getUrl());
}
PopularDestinationGridVideAdapter popularDestinationGridVideAdapter = newPopularDestinationGridVideAdapter(getContext(), destination_list_grid_vieo);
video_Id=destination_list_grid_vieo.get(0).getUrl();
if(YPlayer!=null){
YPlayer.cueVideo(video_Id);
}
// popularDestinationGridVideAdapter.notifyDataSetChanged();System.out.println("Grid" + destination_list_grid_vieo);
horizontalListView.setAdapter(popularDestinationGridVideAdapter);
} catch (JSONException e) {
Toast.makeText(getContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), LENGTH_LONG).show();
}
}) {
@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = newHashMap<String, String>();
// headers.put("Authorization", "Bearer " + access_token);
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};
requestQueue.add(stringRequest);
}
}
This might help you
Post a Comment for "Youtube Video Is Not Stopped When Clicked In Horizontallistview Item"