Skip to content

Commit

Permalink
improved sample readability
Browse files Browse the repository at this point in the history
  • Loading branch information
PierfrancescoSoffritti committed Nov 14, 2017
1 parent 66609af commit cd60c63
Showing 1 changed file with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.google.api.client.extensions.android.http.AndroidHttp;
Expand All @@ -16,19 +15,13 @@
import com.pierfrancescosoffritti.youtubeplayer.player.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayer;
import com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerFullScreenListener;
import com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerInitListener;
import com.pierfrancescosoffritti.youtubeplayer.player.YouTubePlayerView;
import com.pierfrancescosoffritti.youtubeplayer.ui.PlayerUIController;

import java.io.IOException;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -65,7 +58,7 @@ public void onReady() {
}
});

initFullScreenListener(initializedYouTubePlayer);
addFullScreenListenerToPlayer(initializedYouTubePlayer);
initButtonClickListener(initializedYouTubePlayer);

}, true);
Expand All @@ -78,7 +71,7 @@ public void onDestroy() {
youTubePlayerView.release();
}

private void initFullScreenListener(final YouTubePlayer youTubePlayer) {
private void addFullScreenListenerToPlayer(final YouTubePlayer youTubePlayer) {
youTubePlayerView.addFullScreenListener(new YouTubePlayerFullScreenListener() {
@Override
public void onYouTubePlayerEnterFullScreen() {
Expand Down Expand Up @@ -109,37 +102,17 @@ private void initButtonClickListener(final YouTubePlayer youTubePlayer) {
});
}

private void setVideoTitle(final PlayerUIController playerUIController, final String videoId) {
SingleOnSubscribe<String> onSubscribe = new SingleOnSubscribe<String>() {
@Override
public void subscribe(SingleEmitter<String> emitter) throws Exception {
try {
YouTube youTubeDataAPIEndPoint = new YouTube
.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setApplicationName(APP_NAME)
.build();

VideoListResponse videoListResponse = youTubeDataAPIEndPoint
.videos()
.list("snippet")
.setFields("items(snippet(title))")
.setId(videoId)
.setKey(YOUTUBE_DATA_API_KEY)
.execute();

if(videoListResponse.getItems().size() != 1)
throw new RuntimeException("There should be exactly one video with the provided id");

Video video = videoListResponse.getItems().get(0);
String videoTitle = video.getSnippet().getTitle();
emitter.onSuccess(videoTitle);

} catch (IOException e) {
emitter.onError(e);
}
}
};
Single<String> observable = Single.create(onSubscribe);
/**
* This method is called every time a new video is being loaded/cued.
* It uses the YouTube Data APIs to get the video title from the video ID. You can learn more here https://developers.google.com/youtube/v3/docs/videos/list and here https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=snippet&id=6JYIGclVQdw&fields=items(snippet(title))&_h=9&
* The YouTube Data APIs are nothing more then a wrapper over the YouTube REST API.
*
* youTubeDataAPIEndPoint.execute() does network operations, therefore it cannot be executed on the main thread.
* For simplicity I have used RxJava to implement the asynchronous logic. You can you whatever you want: Threads, AsyncTask ecc.
*/
private void setVideoTitle(PlayerUIController playerUIController, String videoId) {

Single<String> observable = getVideoTitleFromYouTubeDataAPIs(videoId);

observable
.subscribeOn(Schedulers.io())
Expand All @@ -149,4 +122,35 @@ public void subscribe(SingleEmitter<String> emitter) throws Exception {
error -> { throw new RuntimeException(error); }
);
}

private Single<String> getVideoTitleFromYouTubeDataAPIs(String videoId) {
SingleOnSubscribe<String> onSubscribe = emitter -> {
try {
YouTube youTubeDataAPIEndPoint = new YouTube
.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setApplicationName(APP_NAME)
.build();

VideoListResponse videoListResponse = youTubeDataAPIEndPoint
.videos()
.list("snippet")
.setFields("items(snippet(title))")
.setId(videoId)
.setKey(YOUTUBE_DATA_API_KEY)
.execute();

if(videoListResponse.getItems().size() != 1)
throw new RuntimeException("There should be exactly one video with the provided id");

Video video = videoListResponse.getItems().get(0);
String videoTitle = video.getSnippet().getTitle();
emitter.onSuccess(videoTitle);

} catch (IOException e) {
emitter.onError(e);
}
};

return Single.create(onSubscribe);
}
}

0 comments on commit cd60c63

Please sign in to comment.