Skip to content

Commit

Permalink
Rework basic obj-c example to support live snippets in the getting st…
Browse files Browse the repository at this point in the history
…arted guide.

PiperOrigin-RevId: 694144415
  • Loading branch information
gschoppe authored and IMA Developer Relations committed Nov 14, 2024
1 parent f977532 commit 4ada05b
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions Objective-C/BasicExample/app/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#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 =
/// Fallback URL in case something goes wrong in loading the stream. This stream is not used, unless
/// an error has occurred.
static NSString *const kBackupContentUrl =
@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

/// Live stream asset key.
Expand All @@ -17,6 +18,17 @@
static NSString *const kVideoID = @"tears-of-steel";

@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
/// Entry point for the SDK. Used to make ad requests.
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
/// The container where the SDK renders each ad's user interface elements.
@property(nonatomic, strong) IMAAdDisplayContainer *adDisplayContainer;
/// An implementation of the `IMA Video Display` protocol. Used to give the SDK access to your video
/// player.
@property(nonatomic, strong) IMAAVPlayerVideoDisplay *imaVideoDisplay;
/// The main point of interaction with the SDK. Created by the SDK as the result of a successful ad
/// request.
@property(nonatomic, strong) IMAStreamManager *streamManager;
// [END import_ima_sdk]

/// Content video player.
@property(nonatomic, strong) AVPlayer *contentPlayer;
Expand All @@ -27,35 +39,17 @@ @interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
/// 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;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.playButton.layer.zPosition = MAXFLOAT;

[self setupAdsLoader];
[self setUpContentPlayer];
}

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

#pragma mark Content Player Setup
self.playButton.layer.zPosition = MAXFLOAT;

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

// Create a player layer for the player.
Expand All @@ -64,29 +58,34 @@ - (void)setUpContentPlayer {
// Size, position, and display the AVPlayer.
playerLayer.frame = self.videoView.layer.bounds;
[self.videoView.layer addSublayer:playerLayer];
}

#pragma mark SDK Setup

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

- (void)requestStream {
// Create an ad display container for ad rendering.
IMAAdDisplayContainer *adDisplayContainer =
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];
self.imaVideoDisplay = [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer];
// [END implement_ads_loader]
}

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

// [START make_stream_request]
- (void)requestStream {
// 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
adDisplayContainer:self.adDisplayContainer
videoDisplay:self.imaVideoDisplay
userContext:nil];
// VOD request. Comment out the IMALiveStreamRequest above and uncomment this IMAVODStreamRequest
// to switch from a livestream to a VOD stream.
Expand All @@ -98,9 +97,9 @@ - (void)requestStream {
userContext:nil];*/
[self.adsLoader requestStreamWithRequest:request];
}
// [END make_stream_request]

#pragma mark AdsLoader Delegates

// [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.
Expand All @@ -115,9 +114,9 @@ - (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorD
adErrorData.adError.message);
[self.contentPlayer 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);
switch (event.type) {
Expand Down Expand Up @@ -161,5 +160,6 @@ - (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdError:(IMAAd
error.message);
[self.contentPlayer play];
}
// [END stream_manager_delegates]

@end

0 comments on commit 4ada05b

Please sign in to comment.