How to Use Huawei Roll Ads with ExoPlayer?
Introduction
Hello everyone,
Today I will talk about using Huawei Roll Ads with Exoplayer in Android projects.
With this solution, you will be able to show video ads on your video player easily.
What is Exoplayer?
Exoplayer is an extensible media player for Android developed by Google.
Check the below link for more details about Exoplayer :
Check the below link for the Github page of Exoplayer :
What is Roll Ads?
Roll ads are displayed as short videos or images, before, during, or after the video content is played.
Check the below link for more details about Huawei Roll Ads:
Preparations Before Coding
Before the development, two things must be done :
1-) Creating an ad unit ID in Huawei Publisher Console
Click here to enter Huawei Publisher Console. After that create an ad unit with Roll ad type.
If you are new to Huawei Ecosystem don’t have an account check the medium post below :
2-) Exoplayer development
Before using the solution you need to complete the exoplayer development.
If you are new to exoplayer, you can check the example code here :
I will also share exoplayer codes in the section “Demo Application”.
Before Start
Software Requirements :
- Android Studio
- JDK 1.7 or later
- minSdkVersion: 19
- Gradle version: 4.1 or later
Integrate The Huawei Roll Ads Adapter
In your project-level build.gradle, include Huawei’s Maven repository.
buildscript {
repositories {
google()
maven { url 'https://jitpack.io' }
mavenCentral()
maven { url 'https://developer.huawei.com/repo/' }}
allprojects {
repositories {
google()
maven { url 'https://jitpack.io' }
mavenCentral()
maven { url 'https://developer.huawei.com/repo/' }
}
}
In the app-level build.gradle, include adapter, exoplayer, and multidex dependencies.
implementation
'com.github.Explore-In-HMS:huawei.ads.exoplayer_adapter:v1.0.0'implementation 'com.google.android.exoplayer:exoplayer:2.15.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.huawei.hms:ads:3.4.49.301'
defaultConfig {
applicationId "com.hms.exoplayerrolladsplugin"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true // enable multidex for the project
}
Demo Application
Finally, we are ready to code. Let's start with our layout.
Create a layout that contains an exoplayer.
I created the player with 200dp it can be created with different sizes, but I recommend using it between 200dp and 400dp.
The UI will be seen better between these sizes.
Important Note: Do not use margin on Playerview, because the library inserts the ads onto this view.
If you need to use margin for the player view, wrap it in Linear Layout, etc., and give margin value to the layout not the player view directly.
Let's continue with our main activity code and the adapter implementation.
The demo code for our solution is like above.
First, we created our Exoplayer and put content inside it. On line 35, you can see the adapter initialization. Let's see how the adapter works.
1-) Constructor Parameters
When creating the builder you must give 3 parameters. Context, player view instance, and your Roll ad unit id.
2-) Set Ad item method
This method is used to specify the video ads according to the placement and skippable value.
Placement
For the placement, There are 5 placements you can choose (Preroll, FirstQuartile, Middle, ThidQuartile, and Post-roll).
The order is not important. according to these placements, the ad will pop up in the video content
For example, you create 2 Ad items, their placements are middle and post-roll. In the middle and at the end of your video content, ads will be shown.
Note: Only one ad will be shown for each placement. Ad pods are not supported currently.
Skippable value
Used to decide if the ad is skippable or not. Give true value for skippable ads.
3-) Optional methods
The methods between lines 43 and 52, used to specify your ad request to target your users better.
I explained them in the code above.
Output
Summary
Now you learned how to implement roll ads with exoplayer easily. You can use this Video ad solution to monetize easily with Huawei Ads.
Thank you for your time :)