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 22, 2024
1 parent f977532 commit 8a6045e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 81 deletions.
115 changes: 57 additions & 58 deletions Objective-C/BasicExample/app/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#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]
/// 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.
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>
/// 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;

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

Expand All @@ -26,36 +32,19 @@ @interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
@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;
// [END_EXCLUDE]

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// [START_EXCLUDE]
self.playButton.layer.zPosition = MAXFLOAT;

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];
NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
self.contentPlayer = [AVPlayer playerWithURL:contentURL];

// Create a player layer for the player.
Expand All @@ -64,43 +53,52 @@ - (void)setUpContentPlayer {
// 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 =
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 import_ima_sdk]

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

- (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
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];*/
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]

#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 +113,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 +159,6 @@ - (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdError:(IMAAd
error.message);
[self.contentPlayer play];
}
// [END stream_manager_delegates]

@end
37 changes: 14 additions & 23 deletions Objective-C/SampleVideoPlayer/SampleVideoPlayer/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
#import "ViewController.h"

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

@interface ViewController ()
/// Non-DAI content URL.
static NSString *const kBackupContentUrl =
@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

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

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

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

@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];
// Load AVPlayer with path to our backup non-DAI content.
NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
self.contentPlayer = [AVPlayer playerWithURL:contentURL];

// Create a player layer for the player.
Expand All @@ -49,4 +35,9 @@ - (void)setUpContentPlayer {
[self.videoView.layer addSublayer:playerLayer];
}

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

@end

0 comments on commit 8a6045e

Please sign in to comment.