/** * Instantiates one of the viewer subclasses depending on the provided configuration. * * @return A new {@link MediaView} instance. */ public static MediaView newInstance(MediaView.Config config_) { ImmutableConfig config = ImmutableConfig.copyOf(config_); Settings settings = Settings.of(config.activity()); if (config.audio() && settings.disableAudio()) config = config.withAudio(false); // handle delay urls first. if (config.mediaUri().hasDelayFlag()) { return new DelayedMediaView(config.withMediaUri(config.mediaUri().withDelay(false))); } if (!config.mediaUri().isLocal()) { config = config.withMediaUri(config.mediaUri().withProxy(true)); } final MediaUri uri = config.mediaUri(); if (uri.getMediaType() == MediaUri.MediaType.VIDEO) { return new VideoMediaView(config); } else if (uri.getMediaType() == MediaUri.MediaType.GIF) { if (shouldUseGifToWebm(uri, settings)) { return new Gif2VideoMediaView(config); } else { return new GifMediaView(config); } } else { return new ImageMediaView(config); } }
private void startWebmConversion(Binder binder, MediaUri url) { logger.info("Start converting gif to webm"); // normalize to http:// String gifUrl = url.toString().replace("https://", "http://"); // and start conversion! conversion = gifToVideoService .toVideo(gifUrl) .compose(binder.get()) .subscribe( result -> { checkMainThread(); // create the correct child-viewer MediaView mediaView; if (result.getVideoUrl().isPresent()) { logger.info("Converted successfully, replace with video player"); MediaUri webm = url.withUri( Uri.parse(result.getVideoUrl().get()), MediaUri.MediaType.VIDEO); mediaView = MediaViews.newInstance(getContext(), binder, webm, this::onMediaShown); } else { logger.info("Conversion did not work, showing gif"); mediaView = new GifMediaView(getContext(), binder, url, this::onMediaShown); } mediaView.removePreviewImage(); setChild(mediaView); }, defaultOnError()); }
private static boolean shouldUseGifToWebm(MediaUri uri, Settings settings) { return !uri.isLocal() && settings.convertGifToWebm(); }