使用 YoutubeAndroidPlayerAPI 的 YoutubePlayerView 播放私人 YouTube 视频?
Play private YouTube video using YoutubePlayerView from YoutubeAndroidPlayerAPI?
我一个月前开始使用 YouTube API Android。我正在尝试制作一个 Android 应用程序,它可以播放一些包含我上传的视频的视频。对于 public 视频,它有效。但是对于私人视频,YoutubePlayerView 显示:"Please sign in".
我不知道如何登录和播放这些视频,因为 YoutubeAndroidPlayer[=25] =]好像不支持认证。
这就是我正在做的事情 "JurB9k3_Ws4" 是我的私人视频 ID。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview_demo);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer,
boolean b) {
youTubePlayer.cueVideo("JurB9k3_Ws4");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
Android 播放器 API 无法做到这一点。在设备上播放私人视频的唯一方法是在您的应用中实现 WebView,让用户登录他们的 YouTube 帐户,然后在 仅在特定浏览器会话中播放私人视频 . YouTube 建议将视频标记为“不公开”而不是“私有”以便在移动设备上播放。
this Google Groups post 中有关使用嵌入式播放器播放私人视频的更多信息。
Playing back a private video in an embedded player will only work if
you have a YouTube login cookie in your browser that corresponds to an
account that is permissioned to watch that video. Otherwise, it will
fail. Authentication with the Data API has nothing to do with whether
you can play the video back in an embedded player.
...
There's no programmatic way to create a login cookie. The user
actually has to login to YouTube.com using the same browser instance
that's using the embed, and that can't be scripted.
只要您的应用程序使用 oath 并经过验证,您就可以构建和播放私人 播放列表,就好像您是 [该帐户的] 帐户的用户一样。使用 youtube v3 api 和 .player api。如果出于某种原因未正确验证,您可以 运行 遇到 api 允许您作为帐户用户创建私人播放列表但列表在播放器 api 中为空的情况。
还有,你居然可以播放私人视频:
@SuppressLint("StaticFieldLeak")
private void loadUploadedVideos() {
if (mChosenAccountName == null) {
return;
}
Log.d(TAG, "loadUploads");
showProgressDialog();
new AsyncTask<Void, Void, List<VideoData>>() {
@Override
protected List<VideoData> doInBackground(Void... voids) {
try {
YouTube youtube = new YouTube.Builder(transport, jsonFactory,
credential).setApplicationName(Constants.APP_NAME)
.build();
/*
* Now that the user is authenticated, the app makes a
* channels list request to get the authenticated user's
* channel. Returned with that data is the playlist id for
* the uploaded videos.
* https://developers.google.com/youtube
* /v3/docs/channels/list
*/
ChannelListResponse clr = youtube.channels()
.list("contentDetails").setMine(true).execute();
// Get the user's uploads playlist's id from channel list
// response
String uploadsPlaylistId = clr.getItems().get(0)
.getContentDetails().getRelatedPlaylists()
.getUploads();
List<VideoData> videos = new ArrayList<VideoData>();
// Get videos from user's upload playlist with a playlist
// items list request
PlaylistItemListResponse pilr = youtube.playlistItems()
.list("id,contentDetails")
.setPlaylistId(uploadsPlaylistId)
.setMaxResults(50L).execute();
List<String> videoIds = new ArrayList<String>();
// Iterate over playlist item list response to get uploaded
// videos' ids.
for (PlaylistItem item : pilr.getItems()) {
videoIds.add(item.getContentDetails().getVideoId());
}
// Get details of uploaded videos with a videos list
// request.
VideoListResponse vlr = youtube.videos()
.list("id,snippet,status")
.setId(TextUtils.join(",", videoIds)).execute();
// Add only the public videos to the local videos list.
for (Video video : vlr.getItems()) {
//if ("public".equals(video.getStatus().getPrivacyStatus())) {
VideoData videoData = new VideoData();
videoData.setVideo(video);
videos.add(videoData);
//}
}
// Sort videos by title
Collections.sort(videos, new Comparator<VideoData>() {
@Override
public int compare(VideoData videoData,
VideoData videoData2) {
return videoData.getTitle().compareTo(
videoData2.getTitle());
}
});
return videos;
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
showGooglePlayServicesAvailabilityErrorDialog(availabilityException
.getConnectionStatusCode());
} catch (UserRecoverableAuthIOException userRecoverableException) {
startActivityForResult(
userRecoverableException.getIntent(),
REQUEST_AUTHORIZATION);
} catch (IOException e) {
Utils.logAndShow(MainActivity.this, Constants.APP_NAME, e);
}
catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<VideoData> videos) {
if (videos == null) {
hideProgressDialog();
return;
}
if (mMyUploadsFragment != null)
mMyUploadsFragment.setUploads(videos);
hideProgressDialog();
}
}.execute((Void) null);
}
同样,一旦通过身份验证,播放器中就可以使用 youtube prime 功能 api。好的。诀窍似乎是将 Youtube 用户登录到 phone 上的 youtube 应用程序,并在您自己的应用程序中使用相同的凭据。
无论如何,应用经过身份验证可以在 android 上播放的任何播放列表都可以这样播放:
public void onPlaylistSelected(PlaylistData playlist) {
try {
mPlaylistData = playlist;
//Intent intent = YouTubeIntents.createPlayPlaylistIntent(this, playlist.getId());
Intent intent = YouTubeIntents.createOpenPlaylistIntent(this, playlist.getId());
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(TAG,e.getMessage());
Toast.makeText(this, R.string.warn_install_yt,Toast.LENGTH_LONG).show();
}
}
我一个月前开始使用 YouTube API Android。我正在尝试制作一个 Android 应用程序,它可以播放一些包含我上传的视频的视频。对于 public 视频,它有效。但是对于私人视频,YoutubePlayerView 显示:"Please sign in".
我不知道如何登录和播放这些视频,因为 YoutubeAndroidPlayer[=25] =]好像不支持认证。
这就是我正在做的事情 "JurB9k3_Ws4" 是我的私人视频 ID。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerview_demo);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer,
boolean b) {
youTubePlayer.cueVideo("JurB9k3_Ws4");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
Android 播放器 API 无法做到这一点。在设备上播放私人视频的唯一方法是在您的应用中实现 WebView,让用户登录他们的 YouTube 帐户,然后在 仅在特定浏览器会话中播放私人视频 . YouTube 建议将视频标记为“不公开”而不是“私有”以便在移动设备上播放。
this Google Groups post 中有关使用嵌入式播放器播放私人视频的更多信息。
Playing back a private video in an embedded player will only work if you have a YouTube login cookie in your browser that corresponds to an account that is permissioned to watch that video. Otherwise, it will fail. Authentication with the Data API has nothing to do with whether you can play the video back in an embedded player.
...
There's no programmatic way to create a login cookie. The user actually has to login to YouTube.com using the same browser instance that's using the embed, and that can't be scripted.
只要您的应用程序使用 oath 并经过验证,您就可以构建和播放私人 播放列表,就好像您是 [该帐户的] 帐户的用户一样。使用 youtube v3 api 和 .player api。如果出于某种原因未正确验证,您可以 运行 遇到 api 允许您作为帐户用户创建私人播放列表但列表在播放器 api 中为空的情况。
还有,你居然可以播放私人视频:
@SuppressLint("StaticFieldLeak")
private void loadUploadedVideos() {
if (mChosenAccountName == null) {
return;
}
Log.d(TAG, "loadUploads");
showProgressDialog();
new AsyncTask<Void, Void, List<VideoData>>() {
@Override
protected List<VideoData> doInBackground(Void... voids) {
try {
YouTube youtube = new YouTube.Builder(transport, jsonFactory,
credential).setApplicationName(Constants.APP_NAME)
.build();
/*
* Now that the user is authenticated, the app makes a
* channels list request to get the authenticated user's
* channel. Returned with that data is the playlist id for
* the uploaded videos.
* https://developers.google.com/youtube
* /v3/docs/channels/list
*/
ChannelListResponse clr = youtube.channels()
.list("contentDetails").setMine(true).execute();
// Get the user's uploads playlist's id from channel list
// response
String uploadsPlaylistId = clr.getItems().get(0)
.getContentDetails().getRelatedPlaylists()
.getUploads();
List<VideoData> videos = new ArrayList<VideoData>();
// Get videos from user's upload playlist with a playlist
// items list request
PlaylistItemListResponse pilr = youtube.playlistItems()
.list("id,contentDetails")
.setPlaylistId(uploadsPlaylistId)
.setMaxResults(50L).execute();
List<String> videoIds = new ArrayList<String>();
// Iterate over playlist item list response to get uploaded
// videos' ids.
for (PlaylistItem item : pilr.getItems()) {
videoIds.add(item.getContentDetails().getVideoId());
}
// Get details of uploaded videos with a videos list
// request.
VideoListResponse vlr = youtube.videos()
.list("id,snippet,status")
.setId(TextUtils.join(",", videoIds)).execute();
// Add only the public videos to the local videos list.
for (Video video : vlr.getItems()) {
//if ("public".equals(video.getStatus().getPrivacyStatus())) {
VideoData videoData = new VideoData();
videoData.setVideo(video);
videos.add(videoData);
//}
}
// Sort videos by title
Collections.sort(videos, new Comparator<VideoData>() {
@Override
public int compare(VideoData videoData,
VideoData videoData2) {
return videoData.getTitle().compareTo(
videoData2.getTitle());
}
});
return videos;
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
showGooglePlayServicesAvailabilityErrorDialog(availabilityException
.getConnectionStatusCode());
} catch (UserRecoverableAuthIOException userRecoverableException) {
startActivityForResult(
userRecoverableException.getIntent(),
REQUEST_AUTHORIZATION);
} catch (IOException e) {
Utils.logAndShow(MainActivity.this, Constants.APP_NAME, e);
}
catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<VideoData> videos) {
if (videos == null) {
hideProgressDialog();
return;
}
if (mMyUploadsFragment != null)
mMyUploadsFragment.setUploads(videos);
hideProgressDialog();
}
}.execute((Void) null);
}
同样,一旦通过身份验证,播放器中就可以使用 youtube prime 功能 api。好的。诀窍似乎是将 Youtube 用户登录到 phone 上的 youtube 应用程序,并在您自己的应用程序中使用相同的凭据。
无论如何,应用经过身份验证可以在 android 上播放的任何播放列表都可以这样播放:
public void onPlaylistSelected(PlaylistData playlist) {
try {
mPlaylistData = playlist;
//Intent intent = YouTubeIntents.createPlayPlaylistIntent(this, playlist.getId());
Intent intent = YouTubeIntents.createOpenPlaylistIntent(this, playlist.getId());
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(TAG,e.getMessage());
Toast.makeText(this, R.string.warn_install_yt,Toast.LENGTH_LONG).show();
}
}