Using Youtube Player Api, Play Video As A Specific User, Applying Youtube Red Subscription
Solution 1:
There is not currently a way to play videos as a specific user, aside from using WebView. It is the same as the answer to Play private YouTube video using YoutubePlayerView from YoutubeAndroidPlayerAPI?:
This is not possible with the Android Player API. The only way to play private videos on a device is to implement a WebView in your app, have the user log into their YouTube account, and then play back the Private video in that specific browser session only.
Solution 2:
As long as you app uses oath and is verified you can build and play private playlists as if you were the user of the account [for that account]. Using the youtube v3 api and the .player api. If not authenticated properly for whatever reason, you can run into situations where the api lets you create a private playlist as a user of the account but the list is empty within the player api.
also, you can actually play private video:
@SuppressLint("StaticFieldLeak")privatevoidloadUploadedVideos() {
if (mChosenAccountName == null) {
return;
}
Log.d(TAG, "loadUploads");
showProgressDialog();
newAsyncTask<Void, Void, List<VideoData>>() {
@Overrideprotected List<VideoData> doInBackground(Void... voids) {
try {
YouTubeyoutube=newYouTube.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
*/ChannelListResponseclr= youtube.channels()
.list("contentDetails").setMine(true).execute();
// Get the user's uploads playlist's id from channel list// responseStringuploadsPlaylistId= clr.getItems().get(0)
.getContentDetails().getRelatedPlaylists()
.getUploads();
List<VideoData> videos = newArrayList<VideoData>();
// Get videos from user's upload playlist with a playlist// items list requestPlaylistItemListResponsepilr= youtube.playlistItems()
.list("id,contentDetails")
.setPlaylistId(uploadsPlaylistId)
.setMaxResults(50L).execute();
List<String> videoIds = newArrayList<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.VideoListResponsevlr= 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())) {VideoDatavideoData=newVideoData();
videoData.setVideo(video);
videos.add(videoData);
//}
}
// Sort videos by title
Collections.sort(videos, newComparator<VideoData>() {
@Overridepublicintcompare(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());
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(List<VideoData> videos) {
if (videos == null) {
hideProgressDialog();
return;
}
if (mMyUploadsFragment != null)
mMyUploadsFragment.setUploads(videos);
hideProgressDialog();
}
}.execute((Void) null);
}
And finally this is the answer to the question, but it needed context:
Also once authenticated, youtube prime features are available in the player api. Nice. The trick seems to be to log the Youtube user into the youtube app on the phone as well as using the same creds in your own app.
anyway any playlist the app is authenticated to play on android can be played this way:
publicvoidonPlaylistSelected(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();
}
}
Post a Comment for "Using Youtube Player Api, Play Video As A Specific User, Applying Youtube Red Subscription"