diff --git a/demos/ima/README.md b/demos/ima/README.md new file mode 100644 index 0000000000..8002b56667 --- /dev/null +++ b/demos/ima/README.md @@ -0,0 +1,4 @@ +# IMA demo application # + +This folder contains a demo application that showcases ExoPlayer integration +with the IMA SDK. diff --git a/demos/ima/build.gradle b/demos/ima/build.gradle new file mode 100644 index 0000000000..d93d826b67 --- /dev/null +++ b/demos/ima/build.gradle @@ -0,0 +1,39 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +apply from: '../../constants.gradle' +apply plugin: 'com.android.application' + +android { + compileSdkVersion project.ext.compileSdkVersion + buildToolsVersion project.ext.buildToolsVersion + defaultConfig { + applicationId "com.google.android.exoplayer2.imademo" + minSdkVersion 16 + targetSdkVersion project.ext.targetSdkVersion + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + } + } +} + +dependencies { + compile 'com.android.support:appcompat-v7:' + supportLibraryVersion + compile project(modulePrefix + 'library-core') + compile project(modulePrefix + 'library-ui') + compile project(modulePrefix + 'extension-ima') +} diff --git a/demos/ima/src/main/AndroidManifest.xml b/demos/ima/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..d6dfe4571e --- /dev/null +++ b/demos/ima/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/DemoPlayer.java b/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/DemoPlayer.java new file mode 100644 index 0000000000..d127304437 --- /dev/null +++ b/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/DemoPlayer.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.imademo; + +import android.content.Context; +import android.net.Uri; +import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.ext.ima.ImaAdsLoader; +import com.google.android.exoplayer2.ext.ima.ImaAdsMediaSource; +import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; +import com.google.android.exoplayer2.extractor.ExtractorsFactory; +import com.google.android.exoplayer2.source.ExtractorMediaSource; +import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.trackselection.TrackSelection; +import com.google.android.exoplayer2.trackselection.TrackSelector; +import com.google.android.exoplayer2.ui.SimpleExoPlayerView; +import com.google.android.exoplayer2.upstream.BandwidthMeter; +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; +import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.util.Util; + +/** + * This class deals with ExoPlayer, the IMA plugin, and all video playback. + */ +class DemoPlayer { + + private final ImaAdsLoader mAdsLoader; + private SimpleExoPlayer mPlayer; + private long mContentPosition; + + DemoPlayer(Context context) { + String adTag = context.getString(R.string.ad_tag_url); + mAdsLoader = new ImaAdsLoader(context, Uri.parse(adTag)); + } + + void init(Context context, SimpleExoPlayerView simpleExoPlayerView) { + // Create a default track selector. + BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + TrackSelection.Factory videoTrackSelectionFactory = + new AdaptiveTrackSelection.Factory(bandwidthMeter); + TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); + + // Create a simple ExoPlayer instance. + mPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector); + + // Bind the player to the view. + simpleExoPlayerView.setPlayer(mPlayer); + + // Produces DataSource instances through which media data is loaded. + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, + Util.getUserAgent(context, context.getString(R.string.app_name))); + // Produces Extractor instances for parsing the media data. + ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); + // This is the MediaSource representing the non-ad, content media to be played. + String contentUrl = context.getString(R.string.content_url); + MediaSource contentMediaSource = new ExtractorMediaSource( + Uri.parse(contentUrl), dataSourceFactory, extractorsFactory, null, null); + // Compose the content media source into a new ImaAdMediaSource with both ads and content. + MediaSource mediaSourceWithAds = new ImaAdsMediaSource( + contentMediaSource, + dataSourceFactory, + mAdsLoader, + simpleExoPlayerView.getOverlayFrameLayout()); + // Prepare the player with the source. + mPlayer.seekTo(mContentPosition); + mPlayer.prepare(mediaSourceWithAds); + mPlayer.setPlayWhenReady(true); + } + + void reset() { + if (mPlayer != null) { + mContentPosition = mPlayer.getContentPosition(); + mPlayer.release(); + } + } + + void release() { + mAdsLoader.release(); + } +} diff --git a/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/MainActivity.java b/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/MainActivity.java new file mode 100644 index 0000000000..6cacdf252f --- /dev/null +++ b/demos/ima/src/main/java/com/google/android/exoplayer2/imademo/MainActivity.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.imademo; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import com.google.android.exoplayer2.ui.SimpleExoPlayerView; + +/** + * Main Activity for the ExoPlayer IMA plugin example. ExoPlayer objects are created by DemoPlayer, + * which this class instantiates. + */ +public class MainActivity extends AppCompatActivity { + + private DemoPlayer mPlayer; + private SimpleExoPlayerView mView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mView = (SimpleExoPlayerView) findViewById(R.id.simpleExoPlayerView); + mPlayer = new DemoPlayer(this); + } + + @Override + public void onResume() { + super.onResume(); + mPlayer.init(this, mView); + } + + @Override + public void onPause() { + super.onPause(); + mPlayer.reset(); + } + + @Override + public void onDestroy() { + mPlayer.release(); + super.onDestroy(); + } +} diff --git a/demos/ima/src/main/res/layout/activity_main.xml b/demos/ima/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000000..180ab3223f --- /dev/null +++ b/demos/ima/src/main/res/layout/activity_main.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/demos/ima/src/main/res/mipmap-hdpi/ic_launcher.png b/demos/ima/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000000..cde69bccce Binary files /dev/null and b/demos/ima/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/demos/ima/src/main/res/mipmap-hdpi/ic_launcher_round.png b/demos/ima/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000000..9a078e3e1a Binary files /dev/null and b/demos/ima/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/demos/ima/src/main/res/mipmap-mdpi/ic_launcher.png b/demos/ima/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000000..c133a0cbd3 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/demos/ima/src/main/res/mipmap-mdpi/ic_launcher_round.png b/demos/ima/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000000..efc028a636 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher.png b/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000000..bfa42f0e7b Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..3af2608a44 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher.png b/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000..324e72cdd7 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..9bec2e6231 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000000..aee44e1384 Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..34947cd6bb Binary files /dev/null and b/demos/ima/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/demos/ima/src/main/res/values/colors.xml b/demos/ima/src/main/res/values/colors.xml new file mode 100644 index 0000000000..5a077b3a78 --- /dev/null +++ b/demos/ima/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3F51B5 + #303F9F + #FF4081 + diff --git a/demos/ima/src/main/res/values/strings.xml b/demos/ima/src/main/res/values/strings.xml new file mode 100644 index 0000000000..9bf928a6b3 --- /dev/null +++ b/demos/ima/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + Exo IMA Demo + + + diff --git a/demos/ima/src/main/res/values/styles.xml b/demos/ima/src/main/res/values/styles.xml new file mode 100644 index 0000000000..705be27764 --- /dev/null +++ b/demos/ima/src/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/settings.gradle b/settings.gradle index f67f091650..d4530d67b7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -20,9 +20,11 @@ if (gradle.ext.has('exoplayerModulePrefix')) { include modulePrefix + 'demo' include modulePrefix + 'demo-cast' +include modulePrefix + 'demo-ima' include modulePrefix + 'playbacktests' project(modulePrefix + 'demo').projectDir = new File(rootDir, 'demos/main') project(modulePrefix + 'demo-cast').projectDir = new File(rootDir, 'demos/cast') +project(modulePrefix + 'demo-ima').projectDir = new File(rootDir, 'demos/ima') project(modulePrefix + 'playbacktests').projectDir = new File(rootDir, 'playbacktests') apply from: 'core_settings.gradle'