Skip to content

Commit

Permalink
Merge pull request #56 from PierfrancescoSoffritti/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
PierfrancescoSoffritti authored Nov 14, 2017
2 parents 44961ad + cd60c63 commit 8fb85d7
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public abstract class AbstractYouTubePlayerListener implements YouTubePlayerList
@Override public void onCurrentSecond(float second) { }
@Override public void onVideoDuration(float duration) { }
@Override public void onMessage(String log) { }
@Override public void onVideoTitle(String videoTitle) { }
// @Override public void onVideoTitle(String videoTitle) { }
@Override public void onVideoId(String videoId) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ public void run() {
});
}

@JavascriptInterface
public void sendVideoTitle(final String videoTitle) {
mainThreadHandler.post(new Runnable() {
@Override
public void run() {
for(YouTubePlayerListener listener : youTubePlayer.getListeners())
listener.onVideoTitle(videoTitle);
}
});
}

// @JavascriptInterface
// public void sendVideoTitle(final String videoTitle) {
// mainThreadHandler.post(new Runnable() {
// @Override
// public void run() {
// for(YouTubePlayerListener listener : youTubePlayer.getListeners())
// listener.onVideoTitle(videoTitle);
// }
// });
// }
//
@JavascriptInterface
public void sendVideoId(final String videoId) {
mainThreadHandler.post(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface YouTubePlayerListener {
void onCurrentSecond(float second);
void onVideoDuration(float duration);
void onMessage(String log);
void onVideoTitle(String videoTitle);
// void onVideoTitle(String videoTitle);
void onVideoId(String videoId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ public DefaultPlayerUIController(@NonNull YouTubePlayerView youTubePlayerView, @
}

@Override
public void showTitle(boolean show) {
public void showVideoTitle(boolean show) {
videoTitle.setVisibility(show ? View.VISIBLE : View.GONE);
}

@Override
public void setVideoTitle(@NonNull String title) {
videoTitle.setText(title);
}

@Override
public void showUI(boolean show) {
if(show)
Expand All @@ -115,7 +120,7 @@ public void enableLiveVideoIndicator(boolean enable) {
* Set custom action to the left of the Play/Pause button
*/
@Override
public void setCustomAction1(Drawable icon, View.OnClickListener clickListener) {
public void setCustomAction1(@NonNull Drawable icon, View.OnClickListener clickListener) {
customActionLeft.setImageDrawable(icon);
customActionLeft.setOnClickListener(clickListener);
showCustomAction1(clickListener != null);
Expand All @@ -125,7 +130,7 @@ public void setCustomAction1(Drawable icon, View.OnClickListener clickListener)
* Set custom action to the right of the Play/Pause button
*/
@Override
public void setCustomAction2(Drawable icon, View.OnClickListener clickListener) {
public void setCustomAction2(@NonNull Drawable icon, View.OnClickListener clickListener) {
customActionRight.setImageDrawable(icon);
customActionRight.setOnClickListener(clickListener);
showCustomAction2(clickListener != null);
Expand All @@ -148,7 +153,7 @@ public void showFullscreenButton(boolean show) {
}

@Override
public void setCustomFullScreenButtonListener(View.OnClickListener customFullScreenButtonListener) {
public void setCustomFullScreenButtonListener(@NonNull View.OnClickListener customFullScreenButtonListener) {
this.onFullScreenButtonListener = customFullScreenButtonListener;
}

Expand Down Expand Up @@ -334,11 +339,6 @@ public void onVideoDuration(float duration) {
seekBar.setMax((int) duration);
}

@Override
public void onVideoTitle(String title) {
videoTitle.setText(title);
}

@Override
public void onVideoId(final String videoId) {
youTubeButton.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -385,21 +385,12 @@ public void onStopTrackingTouch(SeekBar seekBar) {
private void resetUI() {
seekBar.setProgress(0);
seekBar.setMax(0);

videoDuration.post(new Runnable() {
@Override
public void run() {
videoDuration.setText("");
}
});

videoTitle.post(new Runnable() {
@Override
public void run() {
videoTitle.setText("");
}
});

youTubeButton.setOnClickListener(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.pierfrancescosoffritti.youtubeplayer.ui;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

public interface PlayerUIController {
void showTitle(boolean show);
void showUI(boolean show);

void showVideoTitle(boolean show);
void setVideoTitle(@NonNull String videoTitle);

void enableLiveVideoIndicator(boolean enable);

void setCustomAction1(Drawable icon, View.OnClickListener clickListener);
void setCustomAction2(Drawable icon, View.OnClickListener clickListener);
void setCustomAction1(@NonNull Drawable icon, @Nullable View.OnClickListener clickListener);
void setCustomAction2(@NonNull Drawable icon, @Nullable View.OnClickListener clickListener);
void showCustomAction1(boolean show);
void showCustomAction2(boolean show);

void showFullscreenButton(boolean show);
void setCustomFullScreenButtonListener(View.OnClickListener customFullScreenButtonListener);
void setCustomFullScreenButtonListener(@NonNull View.OnClickListener customFullScreenButtonListener);
}
12 changes: 7 additions & 5 deletions YouTubePlayer/src/main/res/raw/youtube_player.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
case YT.PlayerState.PLAYING:
sendStateChange(PLAYING);
timerTaskId = setInterval( () => YouTubePlayerBridge.sendVideoCurrentTime( player.getCurrentTime() ), 100 );
sendVideoData(player.getVideoData());
sendVideoData(player);
return;

case YT.PlayerState.PAUSED:
Expand All @@ -98,12 +98,12 @@
return;
}

function sendVideoData(videoData) {
const { video_id, title } = videoData;
function sendVideoData(player) {
// const { video_id, title } = player.getVideoData();
const videoDuration = player.getDuration();

YouTubePlayerBridge.sendVideoId(video_id)
YouTubePlayerBridge.sendVideoTitle(title);
// YouTubePlayerBridge.sendVideoId(video_id)
// YouTubePlayerBridge.sendVideoTitle(title);
YouTubePlayerBridge.sendVideoDuration(videoDuration);
}

Expand Down Expand Up @@ -134,10 +134,12 @@

function loadVideo(videoId, startSeconds) {
player.loadVideoById(videoId, startSeconds);
YouTubePlayerBridge.sendVideoId(videoId);
}

function cueVideo(videoId, startSeconds) {
player.cueVideoById(videoId, startSeconds);
YouTubePlayerBridge.sendVideoId(videoId);
}

function mute() {
Expand Down
14 changes: 14 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand All @@ -26,4 +31,13 @@ dependencies {
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support:design:27.0.0'
implementation project(':YouTubePlayer')

implementation "io.reactivex.rxjava2:rxjava:2.1.6"
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile('com.google.api-client:google-api-client-android:1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-youtube:v3-rev187-1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package com.pierfrancescosoffritti.youtubeplayersample;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoListResponse;
import com.pierfrancescosoffritti.youtubeplayer.player.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.youtubeplayer.player.PlayerConstants;
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.Single;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

private static final String APP_NAME = "YouTubePlayer_SampleApp";
private static final String YOUTUBE_DATA_API_KEY = "AIzaSyAVeTsyAjfpfBBbUQq4E7jooWwtV2D_tjE";

private YouTubePlayerView youTubePlayerView;
private FullScreenManager fullScreenManager;

Expand All @@ -37,27 +48,19 @@ protected void onCreate(Bundle savedInstanceState) {
fullScreenManager = new FullScreenManager(this);

youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(new YouTubePlayerInitListener() {
@Override
public void onInitSuccess(final YouTubePlayer initializedYouTubePlayer) {

initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
initializedYouTubePlayer.loadVideo(videoIds[0], 0);
}
youTubePlayerView.initialize(initializedYouTubePlayer -> {

@Override
public void onStateChange(@PlayerConstants.PlayerState.State int state) {
Log.d("current state", ""+state);
}
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
initializedYouTubePlayer.loadVideo(videoIds[0], 0);
setVideoTitle(youTubePlayerView.getPlayerUIController(), videoIds[0]);
}
});

});

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

}
}, true);
}

Expand All @@ -68,18 +71,16 @@ 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() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
fullScreenManager.enterFullScreen();

youTubePlayerView.getPlayerUIController().setCustomAction1(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_pause_36dp), new View.OnClickListener() {
@Override
public void onClick(View view) {
if(youTubePlayer != null) youTubePlayer.pause();
}
Drawable icon = ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_pause_36dp);
youTubePlayerView.getPlayerUIController().setCustomAction1(icon, view -> {
if(youTubePlayer != null) youTubePlayer.pause();
});
}

Expand All @@ -94,12 +95,62 @@ public void onYouTubePlayerExitFullScreen() {
}

private void initButtonClickListener(final YouTubePlayer youTubePlayer) {
nextVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String videoId = videoIds[new Random().nextInt(videoIds.length)];
youTubePlayer.loadVideo(videoId, 0);
}
nextVideo.setOnClickListener(view -> {
String videoId = videoIds[new Random().nextInt(videoIds.length)];
youTubePlayer.loadVideo(videoId, 0);
setVideoTitle(youTubePlayerView.getPlayerUIController(), videoId);
});
}

/**
* 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())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
videoTitle -> playerUIController.setVideoTitle(videoTitle),
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 8fb85d7

Please sign in to comment.