summyia haris
summyia haris

Reputation: 121

Youtube Player View not displaying video using with youtube API and channel import

I am using youtube API top import youtube channel in android app. I have created MainActivity, Adapter, Video Datails Class and another activity named youtubePlay activity. the xml for main activity consists of list view and another custom layout with text and image for list view items display obtained from json parsing (title, description, thumbnail). I have added youtube player view in the activity_play_youtube. but on clicking the video from list, the video is not displayed on the youtube player view. the coding is as shown;

MainActivity.java

package com.currentmedia.channel;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;

import com.currentmedia.channel.Adapter.MyCustomAdapter;
import com.currentmedia.channel.Model.VideoDetails;

import static android.widget.Toast.*;

public class MainActivity extends AppCompatActivity {
    //private static final String TAG = "Channel Activity";
    String API_Key = "[]";
    ListView listView;
    ArrayList<VideoDetails> videoDetailsArrayList;
    MyCustomAdapter myCustomAdapter;
    String url="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCVMWWQ985A_-SESZUy_SsVQ&maxResults=50&key=[]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        videoDetailsArrayList = new ArrayList<>();
        myCustomAdapter = new MyCustomAdapter(MainActivity.this, videoDetailsArrayList);

        displayVideos();
    }
        private void displayVideos ()
        {
            RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("items");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            if (jsonObject1.has("id")){
                                JSONObject jsonVideoId=jsonObject1.getJSONObject("id");
                                if (jsonVideoId.has("kind")){
                                    if(jsonVideoId.getString("kind").equals("youtube#video")){
                                        JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
                                        JSONObject jsonObjectDefault=jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");
                                        String video_id=jsonVideoId.getString("videoId");
                                        VideoDetails vd=new VideoDetails();
                                        vd.setVideoId(video_id);
                                        vd.setTitle(jsonObjectSnippet.getString("title"));
                                        vd.setDescription(jsonObjectSnippet.getString("description"));
                                        vd.setUrl(jsonObjectDefault.getString("url"));
                                        videoDetailsArrayList.add(vd);
                                    }
                                    listView.setAdapter(myCustomAdapter);
                                    myCustomAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(),error.getMessage(), LENGTH_LONG).show();
                }
            });
            requestQueue.add(stringRequest);
        }
}

the adapter class named myCustomAdapter is following

package com.currentmedia.channel.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.currentmedia.channel.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import com.currentmedia.channel.Model.VideoDetails;

public class MyCustomAdapter extends BaseAdapter {



    Activity activity;
    ArrayList<VideoDetails> videoDetailsArrayList;
    LayoutInflater inflater;



    public MyCustomAdapter(Activity activity, ArrayList<VideoDetails> videoDetailsArrayList)
    {
        this.activity=activity;
        this.videoDetailsArrayList=videoDetailsArrayList;
    }



    @Override
    public Object getItem(int position) {
        return this.videoDetailsArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return (long) position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(inflater==null)
        {inflater=this.activity.getLayoutInflater();}
        if(convertView == null)
        {convertView=inflater.inflate(R.layout.custom_layout,null);}

        ImageView imageView=(ImageView) convertView.findViewById(R.id.imageView);
        TextView textView=(TextView) convertView.findViewById(R.id.mytitle);

        LinearLayout linearLayout=(LinearLayout)convertView.findViewById(R.id.root);

        final VideoDetails videoDetails=(VideoDetails) this.videoDetailsArrayList.get(position);

        linearLayout.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(activity, com.currentmedia.channel.VideoPlayActivity.class);
                i.putExtra("videoId", videoDetails.getVideoId());
                activity.startActivity(i);}
        });



        Picasso.get().load(videoDetails.getUrl()).into(imageView);

        textView.setText(videoDetails.getTitle());




        return convertView;
    }

    @Override
    public int getCount() {
        return this.videoDetailsArrayList.size();
    }
}

videoPlayActivity.java

package com.currentmedia.channel;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubePlayerView;

public class VideoPlayActivity extends YouTubeBaseActivity {
YouTubePlayerView youTubePlayerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        youTubePlayerView= (YouTubePlayerView) findViewById(R.id.youtube_player) ;
    }
}

and the xml files are : activity_video_play

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoPlayActivity">


    <com.google.android.youtube.player.YouTubePlayerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/youtube_player"></com.google.android.youtube.player.YouTubePlayerView>

</RelativeLayout>

while activity_main and custom_layout is as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"></ListView>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:orientation="horizontal"
        android:id="@+id/root">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView"
                android:src="@mipmap/ic_launcher"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:id="@+id/mytitle"
                android:text="welcome to my video"
                android:textSize="16sp"/>




        </LinearLayout>
    </LinearLayout>


</LinearLayout>

can anybody explain where am I doing wrong?

Upvotes: 3

Views: 1910

Answers (1)

Tanish
Tanish

Reputation: 112

In your videoPlayActivity.java

YouTubePlayerView youTubePlayerView;
            YouTubePlayer.OnInitializedListener onInitializedListener;
    

String url = getIntent().getStringExtra("videoId");
    
    youTubePlayerView = findViewById(R.id.youtube_player);
            onInitializedListener = new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    
                    youTubePlayer.loadVideo(url);
                }
    
                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    
                }
            };
    
            youTubePlayerView.initialize("Api key", onInitializedListener);

To generate Api key https://developers.google.com/youtube/android/player/register

Upvotes: 2

Related Questions