- We create a media player instance that can play a music file when the Activity is loaded as following:
class AudioDemoActivity extends AppCompatActivity {
protected MediaPlayer mMediaPlayer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_native_ad);
// Create MediaPlayer Instance by Audio File in the res/raw Folder
this.mMediaPlayer = MediaPlayer.create(this, R.raw.background_theme);
this.mMediaPlayer.start();
}
}
- We create a native ad instance that will be loaded when
loadNativeAd
is called. For testing purpose, we can fetch for a video test ad by initializing the placement ID with VID_HD_9_16_39S_APP_INSTALL#YOUR_PLACEMENT_ID
. You should fill YOUR_PLACEMENT_ID with the one created from your app dashboard.
Please refer to How to Use Test Mode for more information.
private NativeAd mNativeAd;
protected void loadNativeAd() {
NativeAd nativeAd = new NativeAd(this, "VID_HD_9_16_39S_APP_INSTALL#YOUR_PLACEMENT_ID");
// Initiate a request to load an ad with cache all media
// and a listener to get notified when the ad was loaded.
nativeAd.loadAd(NativeAd.MediaCacheFlag.ALL);
nativeAd.loadAd(
nativeAd.buildLoadAdConfig()
.withAdListener(this)
.withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
.build());
}
-
Implement AudioDemoActivity with
AdListener
and MediaViewListener
.
Declare mAdCoverMediaView
property with MediaView
type in order to show video ad.
When onAdLoaded
is called to AdListener
, set mNativeAd
property to mAdCoverMediaView
property in order to render the video ad. Please refer to Native Ad Integration for more details about showing the native ad in your app.
Implement onVolumeChange
method defined in AdListener
. When a video ad plays with sound, the volume will be greater than 0. The music file playing in your app can be paused. when the video ad is stopped, the volume will be 0 and your app can resume music file playing.
class AudioDemoActivity extends AppCompatActivity implements AdListener, MediaViewListener {
protected MediaView mAdCoverMediaView;
/**
* Ad Listener Implementation
*/
@Override
public void onAdLoaded(Ad ad) {
if (this.mNativeAd != null) {
this.mNativeAd.unregisterView();
}
this.mNativeAd = (NativeAd) ad;
// Create native UI using the ad metadata.
this.mAdCoverMediaView.setNativeAd(this.mNativeAd);
this.mAdCoverMediaView.setListener(this);
// Follow Audience Network Native Ad implementation for creating and rendering other ad assets including ad icon, ad title, ad CTA button and more.
}
/**
* MediaViewListener Implementation
*/
@Override
public void onVolumeChange(MediaView mediaView, float volume) {
if (volume > 0) {
// Pause music playing if video ad plays with sound
this.mMediaPlayer.pause();
} else {
// Resume music playing
this.mMediaPlayer.start();
}
}
}
-
For bringing optimal user experience for showing the rewarded video ad, you should implement
RewardedVideoAdListener
in your activity. You should pause the music file playing before presenting the rewarded video ad in full screen. After the rewarded video ad is closed, you should resume the music file playing. Please refer to Rewarded Video Ad Integration for more details about showing the native ad in your app.
class AudioDemoActivity extends AppCompatActivity implements RewardedVideoAdListener {
protected MediaPlayer mMediaPlayer;
protected RewardedVideoAd mRewardedVideoAd;
/**
* RewardedVideoAdListener Implementation
*/
@Override
public void onRewardedVideoClosed() {
this.mMediaPlayer.start();
}
@Override
public void onAdLoaded(Ad ad) {
this.mMediaPlayer.pause();
// Ad is ready, present it!
this.mRewardedVideoAd.show();
}
}