Youtube 播放器视图不显示与 youtube API 和频道导入一起使用的视频
Youtube Player View not displaying video using with youtube API and channel import
我在 android 应用程序中使用 youtube API 顶部导入 youtube 频道。我创建了 MainActivity、Adapter、Video Datails Class 和另一个名为 youtubePlay activity 的 activity。主要 activity 的 xml 由列表视图和另一个自定义布局组成,其中包含从 json 解析(标题、描述、缩略图)获得的列表视图项目显示的文本和图像。我在 activity_play_youtube 中添加了 youtube 播放器视图。但是单击列表中的视频时,该视频不会显示在 youtube 播放器视图中。
编码如图所示;
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 的适配器 class 正在关注
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_main和custom_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密钥https://developers.google.com/youtube/android/player/register
我在 android 应用程序中使用 youtube API 顶部导入 youtube 频道。我创建了 MainActivity、Adapter、Video Datails Class 和另一个名为 youtubePlay activity 的 activity。主要 activity 的 xml 由列表视图和另一个自定义布局组成,其中包含从 json 解析(标题、描述、缩略图)获得的列表视图项目显示的文本和图像。我在 activity_play_youtube 中添加了 youtube 播放器视图。但是单击列表中的视频时,该视频不会显示在 youtube 播放器视图中。 编码如图所示;
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 的适配器 class 正在关注
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_main和custom_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密钥https://developers.google.com/youtube/android/player/register