fix(android): ads build and enable ads in android sample (#3376)

* fix: refactor androidx core version management

* chore: fix missing import rework for media3

* fix: enable IMA in sample

* chore: rename stub fie

* chore: code review, fix variable name

* chore: reorder imports

* chore: fix linking in sample

* chore: fix stub management

* chore: few cleans and ensure we don't use ima is disabled

---------

Co-authored-by: olivier <olivier.bouillet@ifeelsmart.com>
This commit is contained in:
Olivier Bouillet 2023-11-24 13:17:13 +01:00 committed by GitHub
parent 2648502b36
commit fe89122524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 94 additions and 81 deletions

View File

@ -105,7 +105,7 @@ android {
java { java {
if (useExoplayerIMA) { if (useExoplayerIMA) {
exclude 'com/google/ads/interactivemedia/v3/api' exclude 'com/google/ads/interactivemedia/v3/api'
exclude 'com/google/android/exoplayer2/ext/ima' exclude 'androidx/media3/exoplayer/ima'
} }
} }
} }
@ -143,6 +143,7 @@ repositories {
def media3_version = safeExtGet('media3Version') def media3_version = safeExtGet('media3Version')
def kotlin_version = safeExtGet('kotlinVersion') def kotlin_version = safeExtGet('kotlinVersion')
def androidxCode_version = safeExtGet('androidxCoreVersion')
dependencies { dependencies {
// For < 0.71, this will be from the local maven repo // For < 0.71, this will be from the local maven repo
@ -150,7 +151,7 @@ dependencies {
//noinspection GradleDynamicVersion //noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" implementation "com.facebook.react:react-native:+"
implementation "androidx.core:core:1.9.0" implementation "androidx.core:core:$androidxCode_version"
// For media playback using ExoPlayer // For media playback using ExoPlayer
implementation "androidx.media3:media3-exoplayer:$media3_version" implementation "androidx.media3:media3-exoplayer:$media3_version"

View File

@ -6,3 +6,4 @@ RNVideo_ndkversion=21.4.7075529
RNVideo_buildToolsVersion=30.0.2 RNVideo_buildToolsVersion=30.0.2
RNVideo_media3Version=1.1.1 RNVideo_media3Version=1.1.1
RNVideo_RNVUseExoplayerIMA=false RNVideo_RNVUseExoplayerIMA=false
RNVideo_androidxCoreVersion=1.9.0

View File

@ -0,0 +1,61 @@
package androidx.media3.exoplayer.ima;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.AdViewProvider;
import androidx.media3.common.Player;
import androidx.media3.datasource.DataSpec;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.exoplayer.source.ads.AdsMediaSource;
import com.facebook.react.uimanager.ThemedReactContext;
import java.io.IOException;
public class ImaAdsLoader implements AdsLoader {
public void setPlayer(ExoPlayer ignoredPlayer) {
}
@Override
public void setPlayer(@Nullable Player player) {
}
public void release() {
}
@Override
public void setSupportedContentTypes(@NonNull int... ints) {
}
@Override
public void start(@NonNull AdsMediaSource adsMediaSource, @NonNull DataSpec dataSpec, @NonNull Object adsId, @NonNull AdViewProvider adViewProvider, @NonNull EventListener eventListener) {
}
@Override
public void stop(@NonNull AdsMediaSource adsMediaSource, @NonNull EventListener eventListener) {
}
@Override
public void handlePrepareComplete(@NonNull AdsMediaSource adsMediaSource, int i, int i1) {
}
@Override
public void handlePrepareError(@NonNull AdsMediaSource adsMediaSource, int i, int i1, @NonNull IOException e) {
}
public static class Builder {
public Builder(Context ignoredThemedReactContext) {
}
public Builder setAdEventListener(Object ignoredReactExoplayerView) {
return this;
}
public ImaAdsLoader build() {
return null;
}
}
}

View File

@ -61,6 +61,7 @@ import androidx.media3.exoplayer.drm.FrameworkMediaDrm;
import androidx.media3.exoplayer.drm.HttpMediaDrmCallback; import androidx.media3.exoplayer.drm.HttpMediaDrmCallback;
import androidx.media3.exoplayer.drm.UnsupportedDrmException; import androidx.media3.exoplayer.drm.UnsupportedDrmException;
import androidx.media3.exoplayer.hls.HlsMediaSource; import androidx.media3.exoplayer.hls.HlsMediaSource;
import androidx.media3.exoplayer.ima.ImaAdsLoader;
import androidx.media3.exoplayer.mediacodec.MediaCodecInfo; import androidx.media3.exoplayer.mediacodec.MediaCodecInfo;
import androidx.media3.exoplayer.mediacodec.MediaCodecUtil; import androidx.media3.exoplayer.mediacodec.MediaCodecUtil;
import androidx.media3.exoplayer.smoothstreaming.DefaultSsChunkSource; import androidx.media3.exoplayer.smoothstreaming.DefaultSsChunkSource;
@ -103,7 +104,6 @@ import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
import com.google.ads.interactivemedia.v3.api.AdEvent; import com.google.ads.interactivemedia.v3.api.AdEvent;
import com.google.android.exoplayer2.ext.ima.ImaAdsLoader;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.net.CookieHandler; import java.net.CookieHandler;
@ -617,10 +617,14 @@ public class ReactExoplayerView extends FrameLayout implements
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF); .setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF);
// Create an AdsLoader. // Create an AdsLoader.
adsLoader = new ImaAdsLoader.Builder(themedReactContext).setAdEventListener(this).build(); adsLoader = new ImaAdsLoader.Builder(themedReactContext)
.setAdEventListener(this)
.build();
MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory) DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory);
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView); if (adsLoader != null) {
mediaSourceFactory.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
}
player = new ExoPlayer.Builder(getContext(), renderersFactory) player = new ExoPlayer.Builder(getContext(), renderersFactory)
.setTrackSelector(self.trackSelector) .setTrackSelector(self.trackSelector)
@ -665,8 +669,8 @@ public class ReactExoplayerView extends FrameLayout implements
ArrayList<MediaSource> mediaSourceList = buildTextSources(); ArrayList<MediaSource> mediaSourceList = buildTextSources();
MediaSource videoSource = buildMediaSource(self.srcUri, self.extension, drmSessionManager, cropStartMs, cropEndMs); MediaSource videoSource = buildMediaSource(self.srcUri, self.extension, drmSessionManager, cropStartMs, cropEndMs);
MediaSource mediaSourceWithAds = null; MediaSource mediaSourceWithAds = null;
if (adTagUrl != null) { if (adTagUrl != null && adsLoader != null) {
MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory) DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory)
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView); .setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
DataSpec adTagDataSpec = new DataSpec(adTagUrl); DataSpec adTagDataSpec = new DataSpec(adTagUrl);
mediaSourceWithAds = new AdsMediaSource(videoSource, adTagDataSpec, ImmutableList.of(srcUri, adTagUrl), mediaSourceFactory, adsLoader, exoPlayerView); mediaSourceWithAds = new AdsMediaSource(videoSource, adTagDataSpec, ImmutableList.of(srcUri, adTagUrl), mediaSourceFactory, adsLoader, exoPlayerView);

View File

@ -1,65 +0,0 @@
package com.google.android.exoplayer2.ext.ima;
import androidx.annotation.Nullable;
import androidx.media3.common.AdViewProvider;
import androidx.media3.common.Player;
import androidx.media3.datasource.DataSpec;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.exoplayer.source.ads.AdsMediaSource;
import com.facebook.react.uimanager.ThemedReactContext;
import java.io.IOException;
public class ImaAdsLoader implements AdsLoader {
public void setPlayer(ExoPlayer player) {
}
@Override
public void setPlayer(@Nullable Player player) {
}
public void release() {
}
@Override
public void setSupportedContentTypes(int... ints) {
}
@Override
public void start(AdsMediaSource adsMediaSource, DataSpec dataSpec, Object adsId, AdViewProvider adViewProvider, EventListener eventListener) {
}
@Override
public void stop(AdsMediaSource adsMediaSource, EventListener eventListener) {
}
@Override
public void handlePrepareComplete(AdsMediaSource adsMediaSource, int i, int i1) {
}
@Override
public void handlePrepareError(AdsMediaSource adsMediaSource, int i, int i1, IOException e) {
}
public static class Builder {
public Builder(ThemedReactContext themedReactContext) {
}
public Builder setAdEventListener(Object reactExoplayerView) {
return this;
}
public ImaAdsLoader build() {
return null;
}
}
}

View File

@ -101,7 +101,11 @@ android {
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
} }
} }
configurations.all {
resolutionStrategy { force 'androidx.core:core:1.9.0' }
} }
}
dependencies { dependencies {
// The version of react-native is set by the React Native Gradle Plugin // The version of react-native is set by the React Native Gradle Plugin
@ -120,6 +124,15 @@ dependencies {
} }
implementation project(':react-native-video') implementation project(':react-native-video')
constraints {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
}
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
}
}
} }
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project) apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

View File

@ -30,7 +30,7 @@ public class MainApplication extends Application implements ReactApplication {
@SuppressWarnings("UnnecessaryLocalVariable") @SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages(); List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example: // Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage()); packages.add(new ReactVideoPackage());
return packages; return packages;
} }

View File

@ -6,10 +6,12 @@ buildscript {
minSdkVersion = 21 minSdkVersion = 21
compileSdkVersion = 33 compileSdkVersion = 33
targetSdkVersion = 33 targetSdkVersion = 33
kotlinVersion = "1.6.20" kotlinVersion = "1.8.0"
// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
ndkVersion = "23.1.7779620" ndkVersion = "23.1.7779620"
RNVUseExoplayerIMA = true
} }
repositories { repositories {
google() google()

View File

@ -14,7 +14,6 @@
"@react-native-picker/picker": "^1.9.11", "@react-native-picker/picker": "^1.9.11",
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.72.5", "react-native": "0.72.5",
"react-native-video": "../../",
"react-native-windows": "0.63.41" "react-native-windows": "0.63.41"
}, },
"devDependencies": { "devDependencies": {

View File

@ -715,7 +715,7 @@ class VideoPlayer extends Component {
this.video = ref; this.video = ref;
}} }}
source={this.srcList[this.state.srcListId]} source={this.srcList[this.state.srcListId]}
//adTagUrl={this.srcList[this.state.srcListId]?.adTagUrl} adTagUrl={this.srcList[this.state.srcListId]?.adTagUrl}
style={viewStyle} style={viewStyle}
rate={this.state.rate} rate={this.state.rate}
paused={this.state.paused} paused={this.state.paused}

View File

@ -6129,9 +6129,6 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0" resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w== integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
react-native-video@../../:
version "6.0.0-beta.0"
react-native-windows@0.63.41: react-native-windows@0.63.41:
version "0.63.41" version "0.63.41"
resolved "https://registry.yarnpkg.com/react-native-windows/-/react-native-windows-0.63.41.tgz#96f59bc24749b6c167cb4f35fd74b66f78f4a4bb" resolved "https://registry.yarnpkg.com/react-native-windows/-/react-native-windows-0.63.41.tgz#96f59bc24749b6c167cb4f35fd74b66f78f4a4bb"