Skip to content

Instantly share code, notes, and snippets.

@holysheep
Created October 12, 2015 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holysheep/8085f74a0b12ce24840f to your computer and use it in GitHub Desktop.
Save holysheep/8085f74a0b12ce24840f to your computer and use it in GitHub Desktop.
public void fill() {
if (topTabsTitles.getChildCount() > 0)
return;
Events.get(getActivity()).catalog().onProductOpened(product);
setUpPhotoPager();
setUpProductDetails();
setProductInfo();
product.getMedia().ifPresent(
medias -> Stream.of(medias)
.filter(media -> media.type == ShopItem.Media.Type.video)
.findFirst()
.ifPresent(media -> {
initYoutubePlayer(media.res);
if (errorServicePlay) {
alertPlayServices();
}
videoPlaceHolder.setVisibility(View.VISIBLE);
}));
}
private void alertPlayServices() {
AlertDialog.Builder builder = new AlertDialog.Builder(getDashboardActivity());
builder.setMessage("Отсутствует Youtube плеер")
.setPositiveButton("Установить", (dialog, id) -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.youtube")));
}).setNegativeButton("cancel", (dialog, which) -> {
dialog.dismiss();
});
builder.create().show();
}
private void initYoutubePlayer(@NonNull String videoUrl) {
String youtubeApiKey = Config.get().getGoogleYouTubeApiKey();
String currentUrl = DescriptionDetails.buildVideoId(videoUrl);
YouTubePlayerSupportFragment youTubePlayerFragment = new YouTubePlayerSupportFragment();
getFragmentManager().beginTransaction().add(R.id.fragment_product_video_place, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize(youtubeApiKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (wasRestored) {
youTubePlayer.play();
} else {
videoPlayer = youTubePlayer;
videoPlayer.setOnFullscreenListener(isFullscreen -> {
if (!fullscreen && isFullscreen) {
showFullScreen(currentUrl, youtubeApiKey);
fullscreen = true;
} else if (!isFullscreen)
fullscreen = false;
});
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
youTubePlayer.cueVideo(currentUrl);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "Init fail");
if (youTubeInitializationResult == YouTubeInitializationResult.SERVICE_MISSING) {
errorServicePlay = true;
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment