Skip to content

Commit

Permalink
improved vast demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
superbderrick committed Oct 3, 2017
1 parent 819030f commit c490311
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
23 changes: 22 additions & 1 deletion useCases/vastExample/vastExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="kDk-Gk-YbS">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="kDk-Gk-YbS">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -83,14 +83,35 @@
<constraint firstAttribute="height" constant="240" id="Run-WX-ysn"/>
</constraints>
</view>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="0.5" minValue="0.0" maxValue="1" translatesAutoresizingMaskIntoConstraints="NO" id="X5c-gb-9eN" customClass="SummerSlider" customModule="SummerSlider">
<rect key="frame" x="35" y="531" width="304" height="31"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="FrT-tm-oig"/>
<constraint firstAttribute="width" constant="300" id="fgQ-Lq-SNr"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="markColor">
<color key="value" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="selectedBarColor">
<color key="value" red="0.40000000600000002" green="0.80000001190000003" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="unselectedBarColor">
<color key="value" red="0.8515139222" green="0.73971623180000001" blue="0.35346180199999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</slider>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="6q4-YA-cfJ" firstAttribute="centerY" secondItem="Ppv-f9-jW0" secondAttribute="centerY" id="B9I-s5-Jhk"/>
<constraint firstItem="X5c-gb-9eN" firstAttribute="centerX" secondItem="Ppv-f9-jW0" secondAttribute="centerX" id="Vr6-hR-5vL"/>
<constraint firstItem="6q4-YA-cfJ" firstAttribute="centerX" secondItem="Ppv-f9-jW0" secondAttribute="centerX" id="jT2-AK-Xdv"/>
<constraint firstItem="X5c-gb-9eN" firstAttribute="top" secondItem="6q4-YA-cfJ" secondAttribute="bottom" constant="77.5" id="uCc-UK-Dr3"/>
</constraints>
</view>
<connections>
<outlet property="summerSlider" destination="X5c-gb-9eN" id="dTE-AW-FTR"/>
<outlet property="videoView" destination="6q4-YA-cfJ" id="NGP-bh-p9x"/>
</connections>
</viewController>
Expand Down
94 changes: 84 additions & 10 deletions useCases/vastExample/vastExample/PlayerViewController.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import AVFoundation
import GoogleInteractiveMediaAds
import UIKit

import SummerSlider
class PlayerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {

static let kTestAppContentUrl_MP4 = "http://rmcdn.2mdn.net/Demo/html5/output.mp4"
static let kTestAppContentUrl = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"

static let kTestAppAdTagUrl =
"https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&" +
"iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&" +
"output=vast&unviewed_position_start=1&" +
"cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpost&cmsid=496&vid=short_onecue&" +
"correlator=";

struct POSITION {
static let ZERO : Float = 0.0
static let END : Float = 100.0
}

@IBOutlet weak var videoView: UIView!
var contentPlayer: AVPlayer?
Expand All @@ -14,29 +26,28 @@ class PlayerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManage
var adsLoader: IMAAdsLoader!
var adsManager: IMAAdsManager!

static let kTestAppAdTagUrl =
"https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&" +
"iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&" +
"output=vast&unviewed_position_start=1&" +
"cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=";
@IBOutlet weak var summerSlider: SummerSlider!


override func viewDidLoad() {
super.viewDidLoad()

setUpContentPlayer()
setUpAdsLoader()
requestAds()

// setup For SummerSlider.

setUpSummerSlider()
}

override func viewDidAppear(_ animated: Bool) {
playerLayer?.frame = self.videoView.layer.bounds
}



func setUpContentPlayer() {
// Load AVPlayer with path to our content.
guard let contentURL = URL(string: PlayerViewController.kTestAppContentUrl_MP4) else {
guard let contentURL = URL(string: PlayerViewController.kTestAppContentUrl) else {
print("ERROR: please use a valid URL for the content URL")
return
}
Expand All @@ -56,6 +67,7 @@ class PlayerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManage
selector: #selector(PlayerViewController.contentDidFinishPlaying(_:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: contentPlayer?.currentItem);

}

func contentDidFinishPlaying(_ notification: Notification) {
Expand Down Expand Up @@ -96,6 +108,35 @@ class PlayerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManage

// Initialize the ads manager.
adsManager.initialize(with: adsRenderingSettings)

let duration = getDuration()

var isPreroll = false
var adMarks = Array<Float>();

if self.adsManager != nil {
if self.adsManager!.adCuePoints.count > 0 {
for mark in self.adsManager!.adCuePoints {
let position = mark as! Float
if position == 0 {
isPreroll = true
adMarks.append(POSITION.ZERO)
} else if position == -1 {
adMarks.append(POSITION.END)
} else {
let postionTime :Float64 = Float64(position * 100)
let pointPercent = postionTime / duration
adMarks.append(Float(pointPercent))
}
}
} else if self.adsManager!.adCuePoints.count == 0 {
isPreroll = true
adMarks.append(POSITION.ZERO)
}
}

summerSlider.markPositions = adMarks

}

func adsLoader(_ loader: IMAAdsLoader!, failedWith adErrorData: IMAAdLoadingErrorData!) {
Expand Down Expand Up @@ -128,5 +169,38 @@ class PlayerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManage
// The SDK is done playing ads (at least for now), so resume the content.
contentPlayer?.play()
}

// Get the duration value from the player item.
func getPlayerItemDuration(_ item: AVPlayerItem) -> CMTime {
var itemDuration = kCMTimeInvalid
if (item.responds(to: #selector(getter: CAMediaTiming.duration))) {
itemDuration = item.duration
} else {
if (item.asset.responds(to: #selector(getter: CAMediaTiming.duration))) {
itemDuration = item.asset.duration
}
}
return itemDuration
}

func getDuration() -> Float64 {
var wholeTime :Float64 = 0
if self.contentPlayer != nil {
let controller: PlayerViewController = self
let duration = controller.getPlayerItemDuration(self.contentPlayer!.currentItem!)

wholeTime = CMTimeGetSeconds(duration)
}
return wholeTime
}


// MARK: - Regarding of SummerSlider API.

func setUpSummerSlider() {
// Other properties ​​have already been set from storyboard.
summerSlider.markWidth = 2.0
}

}

0 comments on commit c490311

Please sign in to comment.