use of net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable in project aiode by robinfriedli.
the class PlayableFactory method createPlayables.
/**
* Creates Playables for a Collection of Objects; YouTube videos or Spotify Tracks.
*
* @param redirectSpotify if true the matching YouTube video is loaded to play the full track using
* {@link YouTubeService#redirectSpotify(HollowYouTubeVideo)}, else a {@link PlayableTrackWrapper} is created to play the
* preview mp3 provided by Spotify
* @param items the objects to create a Playable for
* @return the created Playables
*/
public List<Playable> createPlayables(boolean redirectSpotify, Collection<?> items) {
List<Playable> playables = Lists.newArrayList();
List<HollowYouTubeVideo> tracksToRedirect = Lists.newArrayList();
List<YouTubePlaylist> youTubePlaylistsToLoad = Lists.newArrayList();
try {
for (Object item : items) {
if (item instanceof Playable) {
playables.add((Playable) item);
} else if (item instanceof Track) {
handleTrack(SpotifyTrack.wrap((Track) item), redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof Episode) {
handleTrack(SpotifyTrack.wrap((Episode) item), redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof SpotifyTrack) {
handleTrack((SpotifyTrack) item, redirectSpotify, tracksToRedirect, playables);
} else if (item instanceof UrlTrack) {
playables.add(((UrlTrack) item).asPlayable());
} else if (item instanceof YouTubePlaylist) {
YouTubePlaylist youTubePlaylist = ((YouTubePlaylist) item);
playables.addAll(youTubePlaylist.getVideos());
youTubePlaylistsToLoad.add(youTubePlaylist);
} else if (item instanceof PlaylistSimplified) {
List<SpotifyTrack> t = SpotifyInvoker.create(spotifyService.getSpotifyApi()).invoke(() -> spotifyService.getPlaylistTracks((PlaylistSimplified) item));
for (SpotifyTrack track : t) {
handleTrack(track, redirectSpotify, tracksToRedirect, playables);
}
} else if (item instanceof AlbumSimplified) {
List<Track> t = invoker.invoke(() -> spotifyService.getAlbumTracks((AlbumSimplified) item));
for (Track track : t) {
handleTrack(SpotifyTrack.wrapIfNotNull(track), redirectSpotify, tracksToRedirect, playables);
}
} else if (item instanceof AudioTrack) {
playables.add(new UrlPlayable((AudioTrack) item));
} else if (item instanceof AudioPlaylist) {
List<Playable> convertedPlayables = ((AudioPlaylist) item).getTracks().stream().map(UrlPlayable::new).collect(Collectors.toList());
playables.addAll(convertedPlayables);
} else if (item != null) {
throw new UnsupportedOperationException("Unsupported playable " + item.getClass());
}
}
} catch (Exception e) {
throw new RuntimeException("Exception while creating Playables", e);
}
if (!tracksToRedirect.isEmpty() || !youTubePlaylistsToLoad.isEmpty()) {
trackLoadingExecutor.execute(new SpotifyTrackRedirectionRunnable(tracksToRedirect, youTubeService).andThen(new YouTubePlaylistPopulationRunnable(youTubePlaylistsToLoad, youTubeService)));
}
return playables;
}
use of net.robinfriedli.aiode.audio.exec.YouTubePlaylistPopulationRunnable in project aiode by robinfriedli.
the class PlayableFactory method createPlayables.
/**
* Populates a YouTube playlist by fetching the data for each {@link HollowYouTubeVideo} asynchronously and returning
* them as Playables
*
* @param youTubePlaylist the YouTube playlist to load the videos for
*/
public List<Playable> createPlayables(YouTubePlaylist youTubePlaylist) {
List<Playable> playables = Lists.newArrayList(youTubePlaylist.getVideos());
trackLoadingExecutor.execute(new YouTubePlaylistPopulationRunnable(youTubeService, youTubePlaylist));
return playables;
}
Aggregations