Integrating the InStream API

The InStream API is an API for setting up and managing the way InStream ads are loaded and played. It lets you support playing any type of ad break : Pre-roll, Mid-roll, Post-roll, In-roll , and Pause-roll.

Pre-roll, Mid-roll, and Post-roll ad breaks are played using the InstreamAdBinder API. To play In-roll and Pause-roll ad breaks, use the In-roll API and Pause-roll API, respectively.

Note

You can also use the InstreamAdBinder API, In-roll API, and Pause-roll API concurrently if you:

  1. Use different instances of the ad player.
  2. Don't start the Pause-roll and In-roll APIs for playing ads if the main video was paused using the InStreamAdBinder API.

App requirements

  • Use Xcode 15 or higher.

How it works

A loaded InStream ad object contains a schedule for playing ad breaks. Each ad break is described by an InstreamAdBreak object. An ad break may have one of the following types: Pre/Mid/Post/In/Pause-Roll. You can play Pre/MidPost-Roll ad breaks using the InstreamAdBinder API. To play In/Pause-Roll ad breaks, use the In-roll API and Pause-roll API, respectively.

The VideoPlayer protocol is used to interact with the main video content. To play an ad break inside an ad placement, use the InstreamAdPlayer protocol.

InstreamAdBinder tracks the progress of playing the main video and automatically shows ad breaks based on their schedule from a video resource in the Partner interface.

InstreamAdBinder does not directly control the rendering of a video ad in PlayerView. Video ads must be played on the app side based on signals from player interfaces transmitted to InstreamAdBinder. InstreamAdBinder signals the start of playing an ad break by calling VideoPlayer.pauseVideo() and the end by calling VideoPlayer.resumeVideo().

When calling VideoPlayer.pauseVideo() on the app side, it's necessary to hide the main video controls, pause the main video, and start playing the video ad. On the ad SDK side, after calling the method, advertising controls are displayed inside the InstreamAdView container and the InstreamAdPlayer.playAd() method is called to start playing the video ad.

When calling VideoPlayer.resumeVideo() on the app side, it's necessary to return the main video controls and resume playing the main video. On the ad SDK side, ad controls inside the InstreamAdView container are removed before calling the method.

The In/Pause-Roll API doesn't directly control the rendering of a video ad in PlayerView. Video ads must be played on the app side based on signals from player interfaces transmitted to In/Pause-Roll. In/Pause-Roll signals the start of playing an ad break by calling InstreamAdBreakDelegate.instreamAdBreakDidStart() and the end by calling InstreamAdBreakDelegate.instreamAdBreakDidComplete() or InstreamAdBreakDelegate.instreamAdBreakDidError().

When calling InstreamAdBreakDelegate.instreamAdBreakDidStart() on the app side, it's necessary to hide the main video controls and pause the main video. On the ad SDK side, after calling the method, advertising controls are displayed inside the InstreamAdView container and the InstreamAdPlayer.playAd() method is called to start playing the video ad.

When calling InstreamAdBreakDelegate.instreamAdBreakDidComplete() or InstreamAdBreakDelegate.instreamAdBreakDidError() on the app side, it's necessary to return the main video controls and resume playing the main video. On the ad SDK side, ad controls are removed from the InstreamAdView container before calling the methods.

Loading ads

  1. Create an instance of the InstreamAdLoader class to get InStream ads.

  2. To get notifications (ad loaded successfully or failed with an error), subscribe to ad load events. To do this, set a delegate that conforms to the InstreamAdLoaderDelegate protocol.

  3. Create a configuration for the instreamAdRequestConfiguration request using the InstreamAdRequestConfiguration class. Pass the Page ID from the partner interface as a request parameter.

  4. Load ads using the InstreamAdLoader.loadInstreamAdmethod and pass instreamAdRequestConfiguration to it.

To test the integration, use the demo Page ID: demo-instream-vmap-yandex.

adLoader = InstreamAdLoader()
adLoader.delegate = self
let configuration = InstreamAdRequestConfiguration(pageID: PAGE_ID)
adloader.loadInstreamAd(configuration: configuration)

Rendering ads

  1. Implement the InstreamAdPlayer and VideoPlayer interfaces.

    The reference provides detailed information about the methods and their implementation. Additionally, see a test implementation example.

    Tip

    To make implementation easier, we recommend using different instances of players to play video ads and content.

  2. Add InstreamAdView to the View hierarchy of the app. InstreamAdView must contain PlayerView to play video ads in.

    Warning

    A container must be at least 300dp x 160dp in size.

  3. Create an InstreamAdBinder object: pass the loaded InstreamAd object and the InstreamAdPlayer and VideoPlayer implementations to the builder.

    Set up notifications about the ad's progress (ready to play the video ad, the video ad played or failed to play): set a delegate that conforms to the InstreamAdBinderDelegate protocol.

    adBinder = InstreamAdBinder(ad: ad, adPlayer: adPlayer, videoPlayer:
    contentPlayer)
    adBinder.delegate = self
    
  4. To start playing a Pre-roll ad break faster, preload it in advance by calling the InstreamAdBinder.prepareAd() method.

    func preparePrerollAd(adBinder: InstreamAdBinder) {
        adBinder.delegate = self
        adBinder.prepareAd()
    }
    
    extension InstreamViewController: InstreamAdBinderDelegate {
        func instreamAdBinder(_ binder: InstreamAdBinder, didPrepare instreamAd: InstreamAd) {
            addInstreamAdBinderToPreloadedAdQueue(binder)
        }
        //...
    
    }
    
  5. Call the InstreamAdBinder.bind(with adView: InstreamAdView) method for the created InstreamAdBinder object. Pass InstreamAdView, which was previously added to the hierarchy, as a parameter. After that, the InStream SDK starts to automatically track the progress of playing the main video and manage the way video ads are played.

    adBinder.bind(with: instreamAdView)
    
  6. When playing InStream ads in the list, use the InStreamBinder.unbind() method when the cell with the ad is invalidated in the list. To implement a reused pool of players for scrolling, call InstreamAdbinder.invalidateAdPlayer() when reusing the ad player linked to InstreamAdBinder and InstreamAdBinder.invalidateVideoPlayer() when reusing the main content player.

  7. When you stop using InStreamAdBinder, reset the state.

    deinit {
        adBinder.unbind()
        adBinder.invalidateVideoPlayer()
        adBinder.invalidateAdPlayer()
    }
    

Note

Setting up playing Pause-roll ad breaks is similar to In-roll. To do this, replace In-roll classes/methods with Pause-roll ones.

  1. Implement the InstreamAdPlayer interface.

    The reference provides detailed information about the methods and their implementation. Additionally, see a test implementation example.

    Tip

    To make implementation easier, we recommend using different instances of players to play video ads and content.

  2. Add InstreamAdView to the View hierarchy of the app. InstreamAdView must contain PlayerView to play video ads in.

    Warning

    A container must be at least 300dp x 160dp in size.

  3. Use the InstreamAdLoader to load the InstreamAd object using the Page ID from the partner interface.

  4. The InstreamAd object contains a set of different types of ad breaks. To get In-roll ad breaks, use InrollQueueProvider. The InrollQueueProvider queue lets you receive In-roll objects in the display order.

    func instreamAdLoader(_ instreamAdLoader: InstreamAdLoader, didLoad ad:
    InstreamAd) {
        inrollQueue = InrollQueueProvider(ad: ad).queue()
    }
    
  5. To launch the received In-roll object, you need to prepare it. Unprepared In-roll video ads won't start. To track In-roll video ad readiness, set the InstreamAdBreakDelegate, call Inroll.prepare(with: adPlayer), and pass an instance of the InstreamAdPlayer implementation to it.

    private func prepareNextAd() {
        currentInroll = inrollQueue?.poll()
        currentInroll?.delegate = self
        currentInroll?.prepare(with: adPlayer)
    }
    
  6. Once the In-roll video ad is prepared, InstreamAdBreakDelegate.instreamAdBreakDidPrepare() is called. The prepared In-roll video ad is ready to play.

    Tip

    Play video ads in the order they're received from the InrollQueue. If the received In-roll video ads are played in a different order, this may lower your app's monetization.

  7. To play the prepared In-roll video ad, call Inroll.play(with adView: InstreamAdView) and pass InstreamAdView, which was previously added to the View hierarchy, as a parameter.

    func instreamAdBreakDidPrepare(_ adBreak: InstreamAdBreak) {
        currentInroll?.play(instreamAdView)
    }
    
  8. After the ad break starts playing, the InstreamAdBreakDelegate.instreamAdBreakDidStart() method is called. After calling this method, pause the main video and hide its controls.

    func instreamAdBreakDidStart(_ adBreak: InstreamAdBreak) {
        contentVideoPlayer?.pauseVideo()
    }
    
  9. Once the ad break is played, resume playing the main video. A video ad may play successfully or fail. Both situations need to be handled.

    func instreamAdBreakDidComplete(_ adBreak: InstreamAdBreak) {
        handleAdBreakCompleted()
    }
    func instreamAdBreakDidError(_ adBreak: InstreamAdBreak) {
        handleAdBreakCompleted()
    }
    private fun handleAdBreakCompleted() {
        currentInroll = null
        contentVideoPlayer?.resumeVideo()
    }
    
  10. After the current In-roll video ad finishes playing, check the play queue for the next In-roll video ad in the InrollQueue.

    private func prepareNextAd() {
        currentInroll = inrollQueue?.poll()
        currentInroll?.delegate = self
        currentInroll?.prepare(with: adPlayer)
    }
    
  11. When you stop using an In-roll video ad, reset its state.

    deinit {
        currentInroll?.invalidate()
    }