# AV Player with Nowtilus SSAI

### **Step 1: Get the MMGenericFramework Framework Library**

Download the MMGenericFramework faremwork &#x20;

Copy the MMGenericFramework framework to the AVPlayerWithNowtilusSSAI directory

If you are building for the simulator use the MMGenericFramework.framework  in the iphonesimulator-x86 and which building for the device use the  MMGenericFramework.framework in the iphoneos-arm64 directory

### **Step 2: Integrating the MMGeneric Framework with the AV Player Sample Application**&#x20;

There are a few  steps involved for integrating the MediaMelon Player SDK using the MediaMelonSmartStreaming Framework:

1. Add the appropriate MMGenericFramework to the AV Player Sample Application
2. Add the AVPlayerIntegrationWrapper.swift file from the Wrapper directory
3. Provide Asset information
4. Add the MMSSAIAdManagerDelegate callbacks
5. Initializing SSAIAd Manager
6. Set Macro Substitution data
7. Stop the Ad Manager

{% hint style="info" %}
All AV Player related code of the sample application can be found in the Swift class`ViewController.swift`&#x20;
{% endhint %}

#### 1. Import Frameworks

```objectivec
import MMGenericFramework
```

#### &#x20;2. Ad the AVPlayerIntegrationWrapper.swift file

This is the integration layer between the MediaMelon Framework and AV Player. This needs to be added to the project. Unless absolutely necessary , this file should not be modified.

#### 3. Provide Asset information

{% hint style="info" %}
After the instance of the player is create, we should set the asset information and send it to  before starting its playback. In ViewController.swift, call function `self.configureMMSDKwithURL(`player: `AVPlayer)`
{% endhint %}

```swift
private func configureMMSDKWithURL(player: AVPlayer) {
        print("Integrating with \(String(describing: AVPlayerIntegrationWrapper.getVersion()))")

        AVPlayerIntegrationWrapper.shared.mmssaiManagerDelegate = self
        AVPlayerIntegrationWrapper.initializeAssetForPlayer(player: player)
        //Initialize SSAI
        AVPlayerIntegrationWrapper.shared.initialiseSSAIAdManager(mediaUrl: self.mediaURL, isLive: self.isLive, pollForVast: false, vodResponseData: self.vodResponseData)
        AVPlayerIntegrationWrapper.shared.setMacroSubstitution(macroData: dictionary)
 }
```

#### 4.  Add the MMSSAIManagerDelegate

Add the MMSSAIManagerDelegate the ViewController class in ViewController.swift. This delegate will be used for receiving Ad related events and data. At every Ad event the Delegate will fire the notifyMMSSAIAdEventsWith function. This can be used to display Ad related data, enable or disable Ad skipping etc. &#x20;

```swift
class ViewController: UIViewController, MMSSAIManagerDelegate 
.....
     func notifyMMSSAIAdEvents(eventName: MMSSAIAdSate, adInfo: MMSSAIAdDetails) {
        
        if (eventName == MMSSAIAdSate.adPlaying)
        {
            
            print ("Ad progress ", ((AVPlayerIntegrationWrapper.getAdPlaybackTime(adInfo: adInfo)*100)/AVPlayerIntegrationWrapper.getAdDuration(adInfo: adInfo)))
            
        }
        else
        {
            print("---------------------------------------------------")
            print(" Ad Id :",AVPlayerIntegrationWrapper.getAdId(adInfo: adInfo))
            print(" Ad Title :",AVPlayerIntegrationWrapper.getAdTitle(adInfo: adInfo) )
            print(" Ad Index :",AVPlayerIntegrationWrapper.getAdIndex(adInfo: adInfo)," of ", AVPlayerIntegrationWrapper.getAdTotalAdsInPod(adInfo: adInfo) )
            print(" Ad Server :",AVPlayerIntegrationWrapper.getAdServer(adInfo: adInfo))
            print(" Ad Duration :",AVPlayerIntegrationWrapper.getAdDuration(adInfo: adInfo) )
            print(" Ad Position :",AVPlayerIntegrationWrapper.getAdPosition(adInfo: adInfo) )
            print(" Ad Event :",eventName )
            
            if ( eventName == MMSSAIAdSate.adImpression)
            {
                
                let clickURL = AVPlayerIntegrationWrapper.getClickThroughURL(adInfo: adInfo)
                let clickTrackingURL = AVPlayerIntegrationWrapper.getClickTrackingURL(adInfo: adInfo)
                print("Video Click URL:", clickURL)
                print("Video Click Tracking  URL:", clickTrackingURL)
            }
        
            print("---------------------------------------------------")
        }
        
    }
  

```

Ad Events Supported

|                    |                                                 |
| ------------------ | ----------------------------------------------- |
| adCueTimelineStart | Fired at the start of an Ad break               |
| adImpression       | Fired on Ad Impression                          |
| adStarted          | Fired on start of Ad Play                       |
| adPlaying          | Fired every 1s during Ad Playback               |
| adFirstQuartile    | Fired when Ad playback reached the 25% mark     |
| adMidPoint         | Fired when the Ad playback reaches the 50% mark |
| adThirdQuartile    | Fired when Ad Playback reaches the 75% mark     |
| adCompleted        | Fired when Ad Playback reaches 95% mark         |
| adCueTimelineEnd   | Fired at the end of an Ad break                 |
| adSkipped          | Fired when an Ad break is skipped               |

Sample output&#x20;

```
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adCueTimelineStart
---------------------------------------------------
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adImpression
Video Click URL: XXXXXXXXXXXXXXX
Video Click Tracking  URL: XXXXXXXXXXXXXXXXXXX
---------------------------------------------------
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adStarted
---------------------------------------------------
Ad progress  4
Ad progress  8
Ad progress  12
Ad progress  16
Ad progress  20
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adFirstQuartile
---------------------------------------------------
Ad progress  24
Ad progress  28
Ad progress  32
Ad progress  36
Ad progress  40
Ad progress  44
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adMidpoint
---------------------------------------------------
Ad progress  48
Ad progress  52
Ad progress  56
Ad progress  60
Ad progress  64
Ad progress  68
Ad progress  72
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adThirdQuartile
---------------------------------------------------
Ad progress  76
Ad progress  80
Ad progress  84
Ad progress  88
Ad progress  92
Ad progress  96
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adCompleted
---------------------------------------------------
---------------------------------------------------
 Ad Id : 502180
 Ad Title : Video_4
 Ad Index : 1  of  1
 Ad Server : VMAX
 Ad Duration : 25
 Ad Position : MID
 Ad Event : adCueTimelineEnd
---------------------------------------------------

```

**5. Initialize the SSAI Ad Manager**

Initialize the SSAI Ad Manager to track the Ads and report Ad related metric. For live stream the  `isLive` is set to true. The below steps explains how to initialize SSAI Ad Manager without polling for vast. Incase of VOD, STEP 1 and STEP 2 are optional as we get the timeline data from source url. `vodResponseData` should contain the API response from the source URL.

Read the Manifest data from player API and retreive the Vast file from X-AD-VAST tag in the #EXT-X-DATERANGE metadata in the manifest file. To enable this mode there are a a few steps required

STEP 1: Enable Meta Data Collector

```swift
    private func initialisePlayer() {
        self.player = AVPlayer()
        self.metadataCollector = AVPlayerItemMetadataCollector()
        self.playerItem = AVPlayerItem(url: URL(string: self.mediaURL)!)
        
        AVPlayerIntegrationWrapper.setMetaDataCollector( player: self.player,
                                                         collector: self.metadataCollector,
                                                         playerItem: self.playerItem)
      .....
```

STEP 2: Sync player time with Manifest based time

```swift
        override func viewDidLoad() {
        super.viewDidLoad()
        self.getURLDetails()
        self.initialisePlayer()
        
        NotificationCenter.default.addObserver(self,
               selector: #selector(playerItemDidReadyToPlay(notification:)),
               name: .AVPlayerItemNewAccessLogEntry,
                                               object: self.player?.currentItem)
    }
    
    
    .......
    
    
    // Sync player time
    @objc func playerItemDidReadyToPlay(notification: Notification) {
            if let _ = notification.object as? AVPlayerItem {
                
                
                AVPlayerIntegrationWrapper.shared.syncEpochTime(epochTime: Int64(self.playerItem.currentDate()!.timeIntervalSince1970 * 1000))
                
                NotificationCenter.default.removeObserver(self,
                            name: .AVPlayerItemNewAccessLogEntry,
                               object: player?.currentItem)
            }
    }
    
```

STEP 3 Initialise the AD Manager

```swift
AVPlayerIntegrationWrapper.shared.initialiseSSAIAdManager(mediaUrl: self.mediaURL, isLive: self.isLive, pollForVast: false, vodResponseData: self.vodResponseData)  // Poll for vast set to false used the metadatacollector for accesing manifest data
```

#### 6. Set Macro Substitution Data for Tracking URLs

Provide the macro substitution data into a dictionary as shown below and send it to the AVPlayerIntegrationWrapper file.

```swift
let dictionary: [String: Any] = ["ccb": "9281c15a", "seq": 1, "bz": "video", "av": "1.12.3", "cmd": ["key1":"value1", "key2": "value2"]]

AVPlayerIntegrationWrapper.shared.setMacroSubstitution(macroData: dictionary)
```

#### 7. Stop the SSAI Ad Manager &#x20;

To stop the SSAI Ad Manager call the following API

```swift
AVPlayerIntegrationWrapper.shared.stopSSAIAdManager()
```

### List of AvPlayerIntegrationWrapper methods for SSAI

| Method                  | Functionality                                                                            |
| ----------------------- | ---------------------------------------------------------------------------------------- |
| initialiseSSAIAdManager | Initializes the Nowtilus SSAI Ad Manager                                                 |
| setMetaDataCollector    | Configiures the Meta Data collector and then  returns meta data valuessyn                |
| syncEpochTime           | Syncs the player time with the time provided in the Manifest URL                         |
| stopSSAIManager         | Stops the running instance of the SSAI Ad Manager                                        |
| getAdPlaybackTime       | Gets the playback time of the current ad playing ( Called during an Ad Break)            |
| getAdDuration           | Gets duration of the current Ad ( Called during an Ad Break)                             |
| getAdId                 | Gets the Ad Id value( Called during an Ad Break)                                         |
| getAdTitle              | Gets the Ad Title ( Called during an Ad Break)                                           |
| getAdIndex              | Returns in Index of the current ad playing in an Ad break.  ( Called during an Ad Break) |
| getAdTotalAdsInPod      | Gets the toal number of Ads inside an Ad Pod( Called during an Ad Break)                 |
| getAdServer             | Gets the Ad Server Name ( Called during an Ad Break)                                     |
| getAdDuration           | Gets the duration of the events( Called during an Ad Break)                              |
| getAdPosition           | Gets the position of the Ads . For live Ads this will return "MID"                       |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mediamelon.com/mediamelon-jio-ssai/mediamelon-player-sdk-ios/av-player-with-nowtilus-ssai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
