From 5c2f94bda2c4310c2a545893dd7db9c9d1df5ca7 Mon Sep 17 00:00:00 2001 From: Yury Pavlotsky Date: Fri, 19 Jun 2015 10:37:34 -0400 Subject: [PATCH] Commiting latest changes --- .../samplevideoplayer/SampleVideoPlayer.java | 20 +++++++++-- .../samplevideoplayer/VideoPlayer.java | 10 ++++++ .../videoplayerapp/VideoPlayerController.java | 11 +++--- .../VideoPlayerWithAdPlayback.java | 36 +++++++++++++++---- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/SampleVideoPlayer.java b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/SampleVideoPlayer.java index e485ebf3..fa7b3528 100644 --- a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/SampleVideoPlayer.java +++ b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/SampleVideoPlayer.java @@ -104,17 +104,22 @@ public void play() { @Override public void start() { super.start(); - PlaybackState oldPlaybackState = mPlaybackState; - mPlaybackState = PlaybackState.PLAYING; - switch (oldPlaybackState) { + // Fire callbacks before switching playback state. + switch (mPlaybackState) { case STOPPED: for (PlayerCallback callback : mVideoPlayerCallbacks) { callback.onPlay(); } break; + case PAUSED: + for (PlayerCallback callback : mVideoPlayerCallbacks) { + callback.onResume(); + } + break; default: // Already playing; do nothing. } + mPlaybackState = PlaybackState.PLAYING; } @Override @@ -123,6 +128,15 @@ public void stopPlayback() { mPlaybackState = PlaybackState.STOPPED; } + @Override + public void pause() { + super.pause(); + mPlaybackState = PlaybackState.PAUSED; + for (PlayerCallback callback : mVideoPlayerCallbacks) { + callback.onPause(); + } + } + @Override public void addPlayerCallback(PlayerCallback callback) { mVideoPlayerCallbacks.add(callback); diff --git a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/VideoPlayer.java b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/VideoPlayer.java index d3896704..75f3b149 100644 --- a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/VideoPlayer.java +++ b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/VideoPlayer.java @@ -26,6 +26,16 @@ public interface PlayerCallback { * Called when an error occurs during video playback. */ void onError(); + + /** + * Called when the video is paused. + */ + void onPause(); + + /** + * Called when the video is resumed. + */ + void onResume(); } /** diff --git a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerController.java b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerController.java index 9c83ec6b..7b9f3a5a 100644 --- a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerController.java +++ b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerController.java @@ -159,26 +159,29 @@ public void setContentVideo(String videoPath) { } /** - * Starts ad playback + * Starts ad playback. */ public void play() { requestAds(); } /** - * Resumes ad playback + * Resumes video playback. */ public void resume() { mVideoPlayerWithAdPlayback.restorePosition(); - if (mAdsManager != null) { + if (mAdsManager != null && mVideoPlayerWithAdPlayback.getIsAdDisplayed()) { mAdsManager.resume(); } } /** - * Pauses ad playback + * Pauses video playback. */ public void pause() { mVideoPlayerWithAdPlayback.savePosition(); + if (mAdsManager != null && mVideoPlayerWithAdPlayback.getIsAdDisplayed()) { + mAdsManager.pause(); + } } } diff --git a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerWithAdPlayback.java b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerWithAdPlayback.java index 5c5cd3ab..da84c3b8 100644 --- a/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerWithAdPlayback.java +++ b/BasicExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/VideoPlayerWithAdPlayback.java @@ -36,7 +36,7 @@ public interface OnContentCompleteListener { private String mContentVideoUrl; // The saved position in the content to resume to after ad playback. - private int mSavedContentVideoPosition; + private int mSavedVideoPosition; // Called when the content is completed. private OnContentCompleteListener mOnContentCompleteListener; @@ -60,7 +60,7 @@ public VideoPlayerWithAdPlayback(VideoPlayer videoPlayer, ViewGroup adUiContaine public void init() { mIsAdDisplayed = false; - mSavedContentVideoPosition = 0; + mSavedVideoPosition = 0; mIsContentComplete = false; // Define VideoAdPlayer connector. @@ -138,6 +138,24 @@ public void onPlay() { } } + @Override + public void onPause() { + if (mIsAdDisplayed) { + for (VideoAdPlayer.VideoAdPlayerCallback callback : mAdCallbacks) { + callback.onPause(); + } + } + } + + @Override + public void onResume() { + if (mIsAdDisplayed) { + for (VideoAdPlayer.VideoAdPlayerCallback callback : mAdCallbacks) { + callback.onResume(); + } + } + } + @Override public void onError() { if (mIsAdDisplayed) { @@ -183,14 +201,14 @@ public void setContentVideoPath(String contentVideoUrl) { * Save the playback progress state of the currently playing video. */ public void savePosition() { - mSavedContentVideoPosition = mVideoPlayer.getCurrentPosition(); + mSavedVideoPosition = mVideoPlayer.getCurrentPosition(); } /** * Restore the currently loaded video to its previously saved playback progress state. */ public void restorePosition() { - mVideoPlayer.seekTo(mSavedContentVideoPosition); + mVideoPlayer.seekTo(mSavedVideoPosition); } /** @@ -207,10 +225,16 @@ public VideoAdPlayer getVideoAdPlayer() { return mVideoAdPlayer; } - public VideoPlayer getVideoPlayer() { - return mVideoPlayer; + /** + * Returns if an ad is displayed. + */ + public boolean getIsAdDisplayed() { + return mIsAdDisplayed; } + /** + * Returns the content progress provider. + */ public ContentProgressProvider getContentProgressProvider() { return mContentProgressProvider; }