- We create an audio player instance that can play a music file when the view controller is loaded as following:
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"BackgroundTheme" ofType:@"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
[self.audioPlayer play];
}
- We create a native ad instance that will be loaded when
loadNativeAd
is called. For testing purposes, we can fetch 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.
@property (strong, nonatomic) FBNativeAd *_nativeAd;
- (void)loadNativeAd
{
FBNativeAd *nativeAd = [[FBNativeAd alloc] initWithPlacementID:@"VID_HD_9_16_39S_APP_INSTALL#YOUR_PLACEMENT_ID"];
// Set a delegate to get notified when the ad was loaded.
nativeAd.delegate = self;
// Configure native ad to wait to call nativeAdDidLoad: until all ad assets are loaded
nativeAd.mediaCachePolicy = FBNativeAdsCachePolicyAll;
// Initiate a request to load an ad.
[nativeAd loadAd];
}
-
Implement ViewContorller with
FBNativeAdDelegate
and FBMediaViewDelegate
.
Declare adCoverMediaView
property with FBMediaView
type in order to show video ad.
When nativeAdDidLoad
is called to FBNativeAdDelegate
, set nativeAd
property to adCoverMediaView
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 videoVolumeDidChange
method defined in FBMediaViewDelegate
delegate. 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 has stopped, the volume will be 0 and your app can resume music file playing.
@interface ViewController : UIViewController <FBNativeAdDelegate, FBMediaViewDelegate>
@property (weak, nonatomic) IBOutlet FBMediaView *adCoverMediaView;
@end
#pragma mark - FBNativeAdDelegate
- (void)nativeAdDidLoad:(FBNativeAd *)nativeAd
{
NSLog(@"Native ad was loaded, constructing native UI...");
if (self._nativeAd) {
[self._nativeAd unregisterView];
}
self._nativeAd = nativeAd;
// Create native UI using the ad metadata.
[self.adCoverMediaView setNativeAd:nativeAd];
self.adCoverMediaView.delegate = self;
// Follow Audience Network Native Ad implementation for creating and rendering other ad assets including ad icon, ad title, ad CTA button and more.
}
#pragma mark - FBMediaViewDelegate
- (void)mediaView:(FBMediaView *)mediaView videoVolumeDidChange:(float)volume
{
if (volume > 0) {
// Pause music playing if video ad plays with sound
[self.audioPlayer pause];
} else {
// Resume music playing
[self.audioPlayer play];
}
}
-
For bringing optimal user experience for showing the rewarded video ad, you should implement
FBRewardedVideoAdDelegate
in your View Controller. 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.
@interface ViewController : UIViewController <FBRewardedVideoAdDelegate>
@property (nonatomic, strong) FBRewardedVideoAd *rewardedVideoAd;
@end
- (IBAction)showAd
{
[self.audioPlayer pause];
// Ad is ready, present it!
[self.rewardedVideoAd showAdFromRootViewController:self animated:NO];
}
}
- (void)rewardedVideoAdDidClose:(FBRewardedVideoAd *)rewardedVideoAd
{
[self.audioPlayer play];
}