android-studio - Youtube Player View 不显示使用 youtube API 和 channel 导入的视频

标签 android-studio youtubeplayer

我在 Android 应用程序中使用 youtube API top import youtube channel。我创建了 MainActivity、Adapter、Video Datails 类和另一个名为 youtubePlay 的事件。主要事件的 xml 由 ListView 和另一个自定义布局组成,其中包含从 json 解析(标题、描述、缩略图)获得的 ListView 项目显示的文本和图像。我在 activity_play_youtube 中添加了 youtube 播放器 View 。但是单击列表中的视频时,该视频不会显示在 youtube 播放器 View 中。 编码如图所示;

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);
        }
}

名为myCustomAdapter 的适配器类在后面

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) ;
    }
}

xml 文件是: 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>

activity_maincustom_layout如下

<?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>

谁能解释我哪里做错了?

最佳答案

在你的 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);

生成 Api key https://developers.google.com/youtube/android/player/register

关于android-studio - Youtube Player View 不显示使用 youtube API 和 channel 导入的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63753699/

相关文章:

android - YouTubePlayerView 初始化失败

android - 如何修复 'YoutubePlayer stuck on landscapeMode after encountering network error'

android - 构建 android 应用程序给 java.util.zip.zipexception android studio

android - Gradle 同步失败 : Support for builds using Gradle versions older than 2. 6 已删除 [...]。您当前使用的是 Gradle 版本 2.2.1

sqlite - Android Studio 搜索现有的 sqlite 数据库

ios - YouTubePlayer - 更改 iOS 背景颜色

android - youtube_player_flutter : ^7. 0.0+6 未在 iOS 上启用全屏

java - 如何从 Google Places API 将 PlacePicker 的 map 类型从简单更改为卫星?

Android Studio 3.1.0 更新后无法生成数据绑定(bind)