Skip to content

Commit

Permalink
Adds AdvancedExample to demonstrate content playlists and multiple a…
Browse files Browse the repository at this point in the history
…d tags
  • Loading branch information
bretmcg committed Dec 30, 2014
1 parent 0085397 commit 0ad9eeb
Show file tree
Hide file tree
Showing 48 changed files with 1,959 additions and 22 deletions.
25 changes: 11 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
eclipse/assets
eclipse/bin
eclipse/gen
eclipse/libs/*
!eclipse/libs/README
eclipse/.settings
studio/app/build
studio/app/libs/*
!studio/app/libs/README
studio/gradle
studio/.gradle
studio/.idea
studio/local.properties
eclipse/res/values/version.xml
# Built application files
*.apk
*.ap_
*.jar

# Android Studio
.idea
local.properties
*.iml
.gradle
build/
26 changes: 26 additions & 0 deletions AdvancedExample/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.google.ads.interactivemedia.v3.samples.videoplayerapp"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.+'
compile 'com.google.android.gms:play-services:6.5+'
}
5 changes: 5 additions & 0 deletions AdvancedExample/app/libs/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To integrate the IMA SDK, first download the SDK jar from
https://developers.google.com/interactive-media-ads/downloads
and place it in this folder.
Right click the file in Android Studio, select "Add As Library..."
Select the "app" module, and click OK.
33 changes: 33 additions & 0 deletions AdvancedExample/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Add project specific ProGuard rules here.
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# The following rules are used to strip any non essential Google Play Services classes and method.
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
28 changes: 28 additions & 0 deletions AdvancedExample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.ads.interactivemedia.v3.samples.videoplayerapp">

<!-- Required permissions for the IMA SDK -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--This tag is required to use Google Play Services, which the IMA SDK requires -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2014 Google Inc. All Rights Reserved.

package com.google.ads.interactivemedia.v3.samples.samplevideoplayer;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.VideoView;

import java.util.ArrayList;
import java.util.List;

/**
* A VideoView that intercepts various methods and reports them back via a PlayerCallback.
*/
public class SampleVideoPlayer extends VideoView implements VideoPlayer {

private enum PlaybackState {
STOPPED, PAUSED, PLAYING
}

private MediaController mMediaController;
private PlaybackState mPlaybackState;
private final List<PlayerCallback> mVideoPlayerCallbacks = new ArrayList<PlayerCallback>(1);

public SampleVideoPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public SampleVideoPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public SampleVideoPlayer(Context context) {
super(context);
init();
}

private void init() {
mPlaybackState = PlaybackState.STOPPED;
mMediaController = new MediaController(getContext());
mMediaController.setAnchorView(this);
enablePlaybackControls();

// Set OnCompletionListener to notify our callbacks when the video is completed.
super.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Reset the MediaPlayer.
// This prevents a race condition which occasionally results in the media
// player crashing when switching between videos.
disablePlaybackControls();
mediaPlayer.reset();
mediaPlayer.setDisplay(getHolder());
enablePlaybackControls();
mPlaybackState = PlaybackState.STOPPED;

for (PlayerCallback callback : mVideoPlayerCallbacks) {
callback.onCompleted();
}
}
});

// Set OnErrorListener to notify our callbacks if the video errors.
super.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mPlaybackState = PlaybackState.STOPPED;
for (PlayerCallback callback : mVideoPlayerCallbacks) {
callback.onError();
}

// Returning true signals to MediaPlayer that we handled the error. This will
// prevent the completion handler from being called.
return true;
}
});
}

@Override
public void setOnCompletionListener(OnCompletionListener listener) {
// The OnCompletionListener can only be implemented by SampleVideoPlayer.
throw new UnsupportedOperationException();
}

@Override
public void setOnErrorListener(OnErrorListener listener) {
// The OnErrorListener can only be implemented by SampleVideoPlayer.
throw new UnsupportedOperationException();
}

// Methods implementing the VideoPlayer interface.
@Override
public void play() {
start();
}

@Override
public void start() {
super.start();
PlaybackState oldPlaybackState = mPlaybackState;
mPlaybackState = PlaybackState.PLAYING;
switch (oldPlaybackState) {
case STOPPED:
for (PlayerCallback callback : mVideoPlayerCallbacks) {
callback.onPlay();
}
break;
case PAUSED:
for (PlayerCallback callback : mVideoPlayerCallbacks) {
callback.onResume();
}
break;
default:
// Already playing; do nothing.
break;
}
}

@Override
public void pause() {
super.pause();
mPlaybackState = PlaybackState.PAUSED;
for (PlayerCallback callback : mVideoPlayerCallbacks) {
callback.onPause();
}
}

@Override
public void stopPlayback() {
super.stopPlayback();
mPlaybackState = PlaybackState.STOPPED;
}

@Override
public void disablePlaybackControls() {
setMediaController(null);
}

@Override
public void enablePlaybackControls() {
setMediaController(mMediaController);
}

@Override
public void addPlayerCallback(PlayerCallback callback) {
mVideoPlayerCallbacks.add(callback);
}

@Override
public void removePlayerCallback(PlayerCallback callback) {
mVideoPlayerCallbacks.remove(callback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2014 Google Inc. All Rights Reserved.

package com.google.ads.interactivemedia.v3.samples.samplevideoplayer;

/**
* Interface definition for controlling video playback.
*/
public interface VideoPlayer {

/**
* Interface for alerting caller of major video events.
*/
public interface PlayerCallback {

/**
* Called when the current video starts playing from the beginning.
*/
void onPlay();

/**
* Called when the current video pauses playback.
*/
void onPause();

/**
* Called when the current video resumes playing from a paused state.
*/
void onResume();

/**
* Called when the current video has completed playback to the end of the video.
*/
void onCompleted();

/**
* Called when an error occurs during video playback.
*/
void onError();
}

/**
* Play the currently loaded video from its current position.
*/
void play();

/**
* Pause the currently loaded video.
*/
void pause();

/**
* Get the playback progress state (milliseconds) of the current video.
*/
int getCurrentPosition();

/**
* Progress the currently loaded video to the given position (milliseconds).
*/
void seekTo(int videoPosition);

/**
* Get the total length of the currently loaded video in milliseconds.
*/
int getDuration();

/**
* Stop playing the currently loaded video.
*/
void stopPlayback();

/**
* Prevent the media controller (playback controls) from appearing.
*/
void disablePlaybackControls();

/**
* Allow the media controller (playback controls) to appear when appropriate.
*/
void enablePlaybackControls();

/**
* Set the URL or path of the video to play.
*/
void setVideoPath(String videoUrl);

/**
* Provide the player with a callback for major video events (pause, complete, resume, etc).
*/
void addPlayerCallback(PlayerCallback callback);

/**
* Remove a player callback from getting notified on video events.
*/
void removePlayerCallback(PlayerCallback callback);
}
Loading

0 comments on commit 0ad9eeb

Please sign in to comment.