Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions android/build.gradle
freeboub marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ android {
java {
if (useExoplayerIMA) {
exclude 'com/google/ads/interactivemedia/v3/api'
exclude 'com/google/android/exoplayer2/ext/ima'
exclude 'androidx/media3/exoplayer/ima'
}
}
}
Expand Down Expand Up @@ -143,14 +143,15 @@ repositories {

def media3_version = safeExtGet('media3Version')
def kotlin_version = safeExtGet('kotlinVersion')
def androidxCode_version = safeExtGet('androidxCoreVersion')

dependencies {
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"

implementation "androidx.core:core:1.9.0"
implementation "androidx.core:core:$androidxCode_version"

// For media playback using ExoPlayer
implementation "androidx.media3:media3-exoplayer:$media3_version"
Expand Down
3 changes: 2 additions & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ RNVideo_compileSdkVersion=31
RNVideo_ndkversion=21.4.7075529
RNVideo_buildToolsVersion=30.0.2
RNVideo_media3Version=1.1.1
RNVideo_RNVUseExoplayerIMA=false
RNVideo_RNVUseExoplayerIMA=false
RNVideo_androidxCoreVersion=1.9.0
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import androidx.media3.exoplayer.drm.HttpMediaDrmCallback;
import androidx.media3.exoplayer.drm.UnsupportedDrmException;
import androidx.media3.exoplayer.hls.HlsMediaSource;
import androidx.media3.exoplayer.ima.ImaAdsLoader;
import androidx.media3.exoplayer.mediacodec.MediaCodecInfo;
import androidx.media3.exoplayer.mediacodec.MediaCodecUtil;
import androidx.media3.exoplayer.smoothstreaming.DefaultSsChunkSource;
Expand Down Expand Up @@ -103,7 +104,6 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.google.ads.interactivemedia.v3.api.AdEvent;
import com.google.android.exoplayer2.ext.ima.ImaAdsLoader;
freeboub marked this conversation as resolved.
Show resolved Hide resolved
import com.google.common.collect.ImmutableList;

import java.net.CookieHandler;
Expand Down Expand Up @@ -615,10 +615,14 @@ private void initializePlayerCore(ReactExoplayerView self) {
.setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF);

// 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)
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory);
if (adsLoader != null) {
mediaSourceFactory.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
}

player = new ExoPlayer.Builder(getContext(), renderersFactory)
.setTrackSelector(self.trackSelector)
Expand Down Expand Up @@ -663,8 +667,8 @@ private void initializePlayerSource(ReactExoplayerView self, DrmSessionManager d
ArrayList<MediaSource> mediaSourceList = buildTextSources();
MediaSource videoSource = buildMediaSource(self.srcUri, self.extension, drmSessionManager, startTimeMs, endTimeMs);
MediaSource mediaSourceWithAds = null;
if (adTagUrl != null) {
MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory)
if (adTagUrl != null && adsLoader != null) {
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory)
.setLocalAdInsertionComponents(unusedAdTagUri -> adsLoader, exoPlayerView);
DataSpec adTagDataSpec = new DataSpec(adTagUrl);
mediaSourceWithAds = new AdsMediaSource(videoSource, adTagDataSpec, ImmutableList.of(srcUri, adTagUrl), mediaSourceFactory, adsLoader, exoPlayerView);
Expand Down

This file was deleted.

13 changes: 13 additions & 0 deletions examples/basic/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ android {
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
configurations.all {
resolutionStrategy { force 'androidx.core:core:1.9.0' }
}
}


dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")
Expand All @@ -120,6 +124,15 @@ dependencies {
}

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)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new ReactVideoPackage());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KrzysztofMoch This one was missing ! but now it work fine, thank uyou for the tips of removing react-native-video from package.json !

return packages;
}

Expand Down
4 changes: 3 additions & 1 deletion examples/basic/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ buildscript {
minSdkVersion = 21
compileSdkVersion = 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.
ndkVersion = "23.1.7779620"

RNVUseExoplayerIMA = true
}
repositories {
google()
Expand Down
1 change: 0 additions & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@react-native-picker/picker": "^1.9.11",
"react": "18.2.0",
"react-native": "0.72.5",
"react-native-video": "../../",
"react-native-windows": "0.63.41"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class VideoPlayer extends Component {
this.video = ref;
}}
source={this.srcList[this.state.srcListId]}
//adTagUrl={this.srcList[this.state.srcListId]?.adTagUrl}
adTagUrl={this.srcList[this.state.srcListId]?.adTagUrl}
style={viewStyle}
rate={this.state.rate}
paused={this.state.paused}
Expand Down
3 changes: 0 additions & 3 deletions examples/basic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6129,9 +6129,6 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-native-video@../../:
version "6.0.0-beta.0"

[email protected]:
version "0.63.41"
resolved "https://registry.yarnpkg.com/react-native-windows/-/react-native-windows-0.63.41.tgz#96f59bc24749b6c167cb4f35fd74b66f78f4a4bb"
Expand Down