Skip to content

Commit

Permalink
Commiting latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Pavlotsky committed Jun 19, 2015
1 parent 36160ee commit 5c2f94b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,7 +60,7 @@ public VideoPlayerWithAdPlayback(VideoPlayer videoPlayer, ViewGroup adUiContaine

public void init() {
mIsAdDisplayed = false;
mSavedContentVideoPosition = 0;
mSavedVideoPosition = 0;
mIsContentComplete = false;

// Define VideoAdPlayer connector.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down

0 comments on commit 5c2f94b

Please sign in to comment.