AVPlayer (Cocoapods)

This guide provides detailed instructions on integrating the MediaMelon Player SDK into an iOS Media Player Application.

Step 0: Creating AVPlayer based media player application

Create a directory for integration by doing the following. Open the terminal, change directory to the home directory, and create a folder sampleintegration

cd ~/
mkdir sampleintegration
cd sampleintegration/

Pull the Sample Application from https://sdk.mediamelon.com/$CustomerID/SampleAVPlayerWithFreeWheelAds.zip and unzip it

wget https://sdk.mediamelon.com/$CustomerID/SampleAVPlayerWithFreeWheelAds.zip

Get the sample app dependencies by changing the directory to the ~/sampleintegration/SampleAVPlayerWithFreeWheelAds and execute the following commands

Step 1: Building the MediaMelonSmartStreaming Framework

Execute the following two commands on the terminal in the folder ~/sampleintegration/SampleAVPlayerWithFreeWheelAds

sudo gem install cocoapods
pod init
touch Podfile
open Podfile

Edit the Podfile

target 'SwiftDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  pod 'MediaMelonSmartStreaming/AVPlayerWithFreewheel', '~> 1.0.1'
  # Pods for SwiftDemo

Finally, install the pod pod install

Freewheel SDK will be added automatically as a dependency to MediaMelon SDK. If you have added the Freewheel SDK in your project, please remove it

Step 2: Integrating the MediaMelonSmartStreaming Framework

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

  1. Importing the framework

  2. Providing asset information for the content before starting the player and after creating its instance

  3. Cleaning up the SDK integration session

  4. Disable manifest fetching by the SDK

  5. Passing FWContext object

All AVPlayer related code of the sample application can be found in the Swift class DemoViewController.swift

Open the project file ~/sampleintegration/SampleAVPlayerWithFreeWheelAds/SwiftDemo.workspace in XCode and edit the file DemoViewController.swiftas mentioned below

1. Import Frameworks

import AVFoundation
import MediaMelonSmartStreaming
import AdManager

2. Provide Asset information

After the instance of the player is created, and AVAsset is set with it, we should set the asset information and send it to before starting its playback. In DemoViewController.swift, call function self.configureMMSDKwithURL(urlString, self.player)


private func configureMMSDKWithURL(hlsURLStr: String, player: AVPlayer) {
    //Integration Step 2
    print("Integrating with \(String(describing: AVPlayerIntegrationWrapper.getVersion()))")
    AVPlayerIntegrationWrapper.shared.enableLogTrace(logStTrace: true)
    let assetInfo = MMAssetInformation(assetURL: hlsURLStr, assetID: String(arc4random()), assetName: "assetNameRandom", videoId: "VIDEO_ID")
    // Optional Asset metadata
    assetInfo.setContentType("Movie") //Type of content (Movie / Special / Clip / Scene)
    assetInfo.setDrmProtection("Fairplay") //Widevine, Fairplay, Playready etc. Unknown means content is protected, but protection type is unknown. For clear contents, do not set this field
    assetInfo.setEpisodeNumber("10") //Sequence Number of the Episode (string).
    assetInfo.setSeriesTitle("testSeries") //Title of the series
    assetInfo.setSeason("testSeason") //Season For example - Season1,2,3 etc
    assetInfo.setGenre("testGenre") //Genre of the content
    // Optional custom metadata
    assetInfo.addCustomKVP("CustomKVP1Key", "CustomKVP1Value")
    assetInfo.addCustomKVP("CustomKVP2Key", "CustomKVP2Value")
    assetInfo.setQBRMode(.QBRModeDisabled, withMetaURL: nil)

    let registrationInfo = MMRegistrationInformation(customerID: "<your_customer_id>", playerName: "<player_name>")
    registrationInfo.setPlayerInformation(brand: "<player_brand>", model: "<player_model>", version: "<player_version>")
    registrationInfo.setSubscriberInformation(subscriberID: "<subscriber_id>", subscriberType: "<subscriber_type>", subscriberTag: "SubscriberTAG")
    AVPlayerIntegrationWrapper.initializeAssetForPlayer(assetInfo: assetInfo, registrationInformation: registrationInfo, player: player)
    //End of integration Step 2


// Configure MediaMelonSmartStreamingSDK
-(void)_configureMMSDKwithURL: (NSString *)urlString {
    MMAssetInformation * assetInfo = [[MMAssetInformation alloc] initWithAssetURL:urlString assetID:@"RANDOM_ID" assetName:@"Some Title" videoId:@"VIDEO_ID"];
    // Optional Asset metadata
    [assetInfo setContentType:@"Movie"]; //Type of content (Movie / Special / Clip / Scene)
    [assetInfo setDrmProtection:@"Fairplay"]; //Widevine, Fairplay, Playready etc. Unknown means content is protected, but protection type is unknown. For clear contents, do not set this field
    [assetInfo setGenre:@"testGenre"]; //Genre of the content
    [assetInfo setEpisodeNumber:@"10"]; //Sequence Number of the Episode (string).
    [assetInfo setSeriesTitle:@"testSeriesTitle"]; //Title of the series
    [assetInfo setSeason:@"testSeason"]; //Season For example - Season1,2,3 etc
    // Optional custom metadata
    [assetInfo addCustomKVP:@"Key1" :@"Value1"];
    [assetInfo addCustomKVP:@"Key2" :@"Value2"];
    [assetInfo setQBRMode:QBRModeDisabled withMetaURL:nil];
    MMRegistrationInformation* registrationInfo = [[MMRegistrationInformation alloc] initWithCustomerID:@"customer_ID" playerName:@"AVPlayerWithFreeWheelAds"];
    [registrationInfo setSubscriberInformationWithSubscriberID:@"subscriberID" subscriberType:@"subscriberType" subscriberTag:@"SubscriberTAG"];
    [registrationInfo setPlayerInformationWithBrand:@"AVPlayer" model:@"AVPlayer" version:@"0.1"];
    [registrationInfo setDomain:@"DOMAIN_NAME"];
    [AVPlayerIntegrationWrapper disableManifestsFetchWithDisable:YES];
    [AVPlayerIntegrationWrapper initializeAssetForPlayerWithAssetInfo:assetInfo registrationInformation:registrationInfo player:self.player];

3. Cleaning up the SDK Session

We need to clean up the SDK session once the playback completes. The SDK internally manages the cleanup for most of the cases. For example - when playback finishes, or some error is notified.

However, in some error cases, like network reachability issues, the error notification is delayed. And before this error notification is available, the user may trigger another session. Therefore, it is advised to clean up the session once the playback finishes.

We recommend cleanup at the following two places.

  • When the view controller hosting the post roll ad terminates

  • When the player is restarted


5. Disable manifest fetching by the SDK

If your workflow restricts the manifest to be accessible from both player and the MediaMelon Player SDK simultaneously, then, you can disable the fetch of manifest via disableManifestsFetch() in method _configureMMSDKwithURL()

private func configureMMSDKWithURL(hlsURLStr: String) {
    AVPlayerIntegrationWrapper.disableManifestsFetch(disable: true)
    AVPlayerIntegrationWrapper.initializeAssetForPlayer(assetInfo: assetInfo, registrationInformation: registrationInfo, player: player)

Note: Disable Manifest Fetch is Optional

5. Passing FWContext object

Provide FWContext object to the MediaMelon Player SDK for the Freewheel ad analytics in the viewDidAppear method as shown below

override func viewDidAppear(_ animated: Bool) {
    AVPlayerIntegrationWrapper.setFreeWheelContext(_adContext: self.adController.adContext)	

Last updated