Skip to content

Commit

Permalink
Add region tags to the Objective-C example for live snippets in the G…
Browse files Browse the repository at this point in the history
…etting started guide.

These region tags are used in the Getting started guide for DAI Full service at https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/ios?service=full

PiperOrigin-RevId: 694144415
  • Loading branch information
gschoppe authored and IMA Developer Relations committed Dec 2, 2024
1 parent f977532 commit 80fcd85
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Objective-C/BasicExample/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '14'

target 'BasicExample' do
pod 'GoogleAds-IMA-iOS-SDK', '~> 3.18.4'
pod 'GoogleAds-IMA-iOS-SDK', '~> 3.23.0'
end
139 changes: 68 additions & 71 deletions Objective-C/BasicExample/app/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

// [START import_ima_sdk]
@import GoogleInteractiveMediaAds;

/// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will not
/// be used.
static NSString *const kTestAppContentUrl_M3U8 =
// [START_EXCLUDE]
/// The backup stream is only played when an error is detected during the stream creation.
static NSString *const kBackupContentUrl =
@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

/// Live stream asset key.
static NSString *const kAssetKey = @"c-rArva4ShKVIAkNfy6HUQ";
/// VOD content source ID.
static NSString *const kContentSourceID = @"2548831";
/// VOD video ID.
static NSString *const kVideoID = @"tears-of-steel";
// [END_EXCLUDE]

@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
/// The entry point for the IMA DAI SDK to make DAI stream requests.
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
/// The container where the SDK renders each ad's user interface elements and companion slots.
@property(nonatomic, strong) IMAAdDisplayContainer *adDisplayContainer;
/// The reference of your video player for the IMA DAI SDK to monitor playback and handle timed
/// metadata.
@property(nonatomic, strong) IMAAVPlayerVideoDisplay *imaVideoDisplay;
/// References the stream manager from the IMA DAI SDK after successful stream loading.
@property(nonatomic, strong) IMAStreamManager *streamManager;

/// Content video player.
@property(nonatomic, strong) AVPlayer *contentPlayer;

// UI
// [START_EXCLUDE]
/// Play button.
@property(nonatomic, weak) IBOutlet UIButton *playButton;
/// UIView in which we will render our AVPlayer for content.
@property(nonatomic, weak) IBOutlet UIView *videoView;

// SDK
/// Entry point for the SDK. Used to make ad requests.
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
/// Main point of interaction with the SDK. Created by the SDK as the result of an ad request.
@property(nonatomic, strong) IMAStreamManager *streamManager;
@property(nonatomic, weak) IBOutlet UIView *videoView;
/// Video player to play the DAI stream for both content and ads.
@property(nonatomic, strong) AVPlayer *videoPlayer;
// [END_EXCLUDE]

@end

Expand All @@ -40,86 +37,85 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

// [START_EXCLUDE]
self.playButton.layer.zPosition = MAXFLOAT;

[self setupAdsLoader];
[self setUpContentPlayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
[self requestStream];
self.playButton.hidden = YES;
}

#pragma mark Content Player Setup

- (void)setUpContentPlayer {
// Load AVPlayer with path to our content.
NSURL *contentURL = [NSURL URLWithString:kTestAppContentUrl_M3U8];
self.contentPlayer = [AVPlayer playerWithURL:contentURL];
// Load AVPlayer with path to your content.
NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
self.videoPlayer = [AVPlayer playerWithURL:contentURL];

// Create a player layer for the player.
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.contentPlayer];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];

// Size, position, and display the AVPlayer.
playerLayer.frame = self.videoView.layer.bounds;
[self.videoView.layer addSublayer:playerLayer];
}

#pragma mark SDK Setup
// [END_EXCLUDE]

- (void)setupAdsLoader {
self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
self.adsLoader.delegate = self;
}

- (void)requestStream {
// Create an ad display container for ad rendering.
IMAAdDisplayContainer *adDisplayContainer =
// Create an ad display container for rendering each ad's user interface elements and companion
// slots.
self.adDisplayContainer =
[[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
viewController:self
companionSlots:nil];

// Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
IMAAVPlayerVideoDisplay *imaVideoDisplay =
[[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer];
// Create a stream request. Use one of "Live stream request" or "VOD request".
// Live stream request.
IMALiveStreamRequest *request = [[IMALiveStreamRequest alloc] initWithAssetKey:kAssetKey
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil];
// VOD request. Comment out the IMALiveStreamRequest above and uncomment this IMAVODStreamRequest
// to switch from a livestream to a VOD stream.
/*IMAVODStreamRequest *request =
[[IMAVODStreamRequest alloc] initWithContentSourceID:kContentSourceID
videoID:kVideoID
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil];*/
[self.adsLoader requestStreamWithRequest:request];
self.imaVideoDisplay = [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.videoPlayer];
}
// [END import_ima_sdk]

// [START make_stream_request]
- (IBAction)onPlayButtonTouch:(id)sender {
[self requestStream];
self.playButton.hidden = YES;
}

#pragma mark AdsLoader Delegates
- (void)requestStream {
// Create a stream request. Use one of "Live stream request" or "VOD request", depending on your
// type of stream.
IMAStreamRequest *request;
// Switch this variable to NO to make a VOD request.
BOOL useLiveStream = YES;
if (useLiveStream) {
// Live stream request. Replace the asset key with your value.
request = [[IMALiveStreamRequest alloc] initWithAssetKey:@"c-rArva4ShKVIAkNfy6HUQ"
adDisplayContainer:self.adDisplayContainer
videoDisplay:self.imaVideoDisplay
userContext:nil];
} else {
// VOD request. Replace the content source ID and video ID with your values.
request = [[IMAVODStreamRequest alloc] initWithContentSourceID:@"2548831"
videoID:@"tears-of-steel"
adDisplayContainer:self.adDisplayContainer
videoDisplay:self.imaVideoDisplay
userContext:nil];
}
[self.adsLoader requestStreamWithRequest:request];
}
// [END make_stream_request]

// [START ads_loader_delegates]
- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
NSLog(@"Stream created with: %@.", adsLoadedData.streamManager.streamId);
// adsLoadedData.streamManager is set because we made an IMAStreamRequest.
self.streamManager = adsLoadedData.streamManager;
self.streamManager.delegate = self;
[self.streamManager initializeWithAdsRenderingSettings:nil];
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
// Something went wrong loading ads. Log the error and play the content.
// Log the error and play the content.
NSLog(@"AdsLoader error, code:%ld, message: %@", adErrorData.adError.code,
adErrorData.adError.message);
[self.contentPlayer play];
[self.videoPlayer play];
}
// [END ads_loader_delegates]

#pragma mark StreamManager Delegates

// [START stream_manager_delegates]
- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event {
NSLog(@"StreamManager event (%@).", event.typeString);
NSLog(@"Ad event (%@).", event.typeString);
switch (event.type) {
case kIMAAdEvent_STARTED: {
// Log extended data.
Expand Down Expand Up @@ -159,7 +155,8 @@ - (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAd
- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdError:(IMAAdError *)error {
NSLog(@"StreamManager error with type: %ld\ncode: %ld\nmessage: %@", error.type, error.code,
error.message);
[self.contentPlayer play];
[self.videoPlayer play];
}
// [END stream_manager_delegates]

@end
45 changes: 17 additions & 28 deletions Objective-C/SampleVideoPlayer/SampleVideoPlayer/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
#import "ViewController.h"

@import AVFoundation;
#import <AVKit/AVKit.h>

@interface ViewController ()

/// Content video player.
@property(nonatomic, strong) AVPlayer *contentPlayer;
/// Content URL.
static NSString *const kBackupContentUrl =
@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

@interface ViewController ()
/// Play button.
@property(nonatomic, weak) IBOutlet UIButton *playButton;

/// UIView in which we will render our AVPlayer for content.
@property(nonatomic, weak) IBOutlet UIView *videoView;

/// Video player.
@property(nonatomic, strong) AVPlayer *videoPlayer;
@end

@implementation ViewController

// The content URL to play.
NSString *const kTestAppContentUrl_MP4 =
@"https://storage.googleapis.com/gvabox/media/samples/android.mp4";

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];

self.playButton.layer.zPosition = MAXFLOAT;

[self setUpContentPlayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
[self.contentPlayer play];
self.playButton.hidden = YES;
}

#pragma mark Content Player Setup

- (void)setUpContentPlayer {
// Load AVPlayer with path to our content.
NSURL *contentURL = [NSURL URLWithString:kTestAppContentUrl_MP4];
self.contentPlayer = [AVPlayer playerWithURL:contentURL];
// Load AVPlayer with the path to your content.
NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
self.videoPlayer = [AVPlayer playerWithURL:contentURL];

// Create a player layer for the player.
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.contentPlayer];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];

// Size, position, and display the AVPlayer.
playerLayer.frame = self.videoView.layer.bounds;
[self.videoView.layer addSublayer:playerLayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
[self.videoPlayer play];
self.playButton.hidden = YES;
}

@end

0 comments on commit 80fcd85

Please sign in to comment.