Code Snippets using the Native SDK for iOS

In this topic, you will find a collection of code snippets which you may find useful as a reference while developing with the SDK. For more detailed solutions, see the iOS player samples.

Table of contents

Advertising

Analytics

Captions

Content security (DRM)

Cue points

Playback

Playlists

Styling

Custom controls for AirPlay

If you use the Brightcove PlayerUI controls, you are all set. AirPlay functionality works out-of-the-box, allowing users to stream video to high-definition displays with Apple TV.

If you are using custom controls, you can follow these steps:

  1. Learn about AirPlay in Apple's developer documentation for AirPlay.

  2. Use the playback controller's allowsExternalPlayback property to set the AVPlayer's allowsExternalPlayback property:

  3. Set up an AirPlay router control and handle its selection. For details, see Apple's AirPlay Overview document.

Reference

For details, see the BCOVPlaybackController documentation.

Customizing the closed captions button

The BCOVPlayerUI sample code shows you how to customize the Brightcove player when using the Native SDK for iOS. For more details, see the Customizing PlayerUI controls section of the Native SDK reference document.

To customize the closed captions button using the PlayerUI, follow these steps:

  1. Start with the Basic Video Playback App.

  2. Replace your values for the Policy Key, Account Id and Video Id. Select a video from your account that has text tracks.

  3. Set up the player view with a standard VOD layout:

  4. The closedCaptionButton is declared as a BCOVPUIButton, which is a subclass of UIButton and adds three additional methods for customization. Whenever you customize BCOVPlayerUI controls, you should use the Native Player APIs wherever they are available. Your customized code should look similar to this:

Displaying FairPlay content on an external screen

When an external display is connected to an iOS device using an AV adapter and HDMI cable, the default behavior is to mirror the iOS screen. The exception to this is when you use FairPlay-protected video, which Apple prevents from mirroring (WWDC 2015, Session 502).

To display FairPlay-protected videos, set the AVPlayer properties exposed through the Brightcove Playback Controller to allow FairPlay video to play on an external display. The video plays in full screen mode. Here is an example of setting these properties:

Reference

For details, see the BCOVPlaybackController documentation.

Google Analytics

If you use the Brightcove player and the catalog class, video analytics will be automatically collected and will appear in your Video Cloud Analytics module. For additional metrics, you can add Google Analytics to your app.

To integrate Google Analytics with your app, follow these steps:

  1. Review Google's document to Add Analytics to Your iOS App.
  2. Here is one way you could use Google Analytics to track video playback using Google's Firebase SDK:

 

Limiting the bitrate

You can't control which source (rendition) in the HLS manifest gets selected by the AVPlayer , but you can put a bitrate cap on playback. This prevents the player from using sources (renditions) with a bitrate over the specified bitrate.

Set the preferredPeakBitRate to the desired limit, in bits per second, of the network bandwidth consumption for the given AVPlayerItem .

Use one of the following declarations:

Looping a video

In some cases, you may want a video to automatically replay. To do this, you can get the "end of video" lifecycle event, seek to the beginning and play again.

This code assumes that you have set the delegate of the playbackController to the object with this method:

Managing videos in a playlist

One way to manage a playlist of videos is to store the video objects in a table. When the user selects a video from the table, the table row will contain the video object.

Here's an overview of how it works:

  1. Retrieve a playlist from your account.

  2. Re-initialize the containers that store information related to the videos in the current playlist.

  3. When the table view is selected, the row's index is used to create a new videoDictionary. Next, ask the dictionary for the video. If the video is not null, then load the video in the playbackController.

To work with playlists, you can store the playlist in another object such as a table. Based on user interaction, you can navigate the object's indices and select the appropriate video.

Media progress values

During media playback, the values reported to the Player SDK progress delegate method may include an initial value of negative infinity and a final value of positive infinity. These values are used when processing pre-roll and post-roll ads.

If these values are not important to you or interfere with your own progress tracking, they can be easily ignored with a conditional statement like this:

Reference

For details, see the BCOVPlaybackController documentation.

Modifying captions programmatically

You can set the captions anytime during playback, after the Ready event has been received. To do this, you can use the BCOVPlaybackControllerDelegate.

Here's an example of setting the captions language to Spanish:

Paging with the Playback API

When retrieving your Video Cloud content from the Playback API, you can implement paging for a playlist.

To page through a set of videos in a playlist, use the following request URL parameters:

  • limit - defines the number of videos to return from the Playback API
  • offset - sets the number of videos to skip in a playlist from the Playback API

This example returns 6 videos starting with the 10th video in the playlist:

Programmatically adding cue points

Video Cloud customers can add cue points to a video using Video Cloud Studio as shown in the Adding Cue Points to Videos document.

You can also add cue points to your video programmatically. The code below adds quarterly interval cue points to the video returned from the Playback API:

 

Note that the value of your cue point type can be any string value that you want, as long as you are not using any of the iOS plugins. For details, see the BCOVCuePoint Protocol Reference document.

If you are using cue points with the IMA plugin, learn more about it in the VAST and VMAP/Server Side Ad Rules section of the IMA Plugin for the Native SDK for iOS notes. The IMA sample app shows you the value required for IMA ad cue points.

The code below listens for your cue points and displays a message:

References

For details, see the following:

Removing the player

There may be cases where you want to remove the player and the view.

Deallocating the view controller that has ownership of a BCOVPlaybackController will deallocate the playback controller as well. To do this, remove the player view from its superview and set their playback controller pointer to nil.

Here is a code example:

Setting audio behavior

The audio session handles audio behavior at the app level. You can choose from several audio session categories and settings to customize your app's audio behavior.

Choose the best audio session category for your app. For details, review Apple's documentation:

Basic sample

For our basic sample, we use AVAudioSessionCategoryPlayback. This plays audio even when the screen is locked and with the Ring/Silent switch set to silent. To keep it simple, we put the code for this in the App Delegate.

Mix with other audio

You may want to allow audio from other apps to be heard when the audio in your app is muted. To do this, you can configure the AVAudioSession in the view controller that has access to your current AVPlayer.

For details, see the mixWithOthers category option.

Setting the playback rate

To control the playback rate, you can set the rate property on the AVPlayer class exposed on the session.

By default, the play rate can only be set on regular intervals (0.50, 0.67, 1.0, 1.25, 1.50 and 2.0). By setting the audioTimePitchAlgorithm, you can use more granular rate values (like 1.7). For more details, see this stackoverflow discussion.

avPlayerItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithmVarispeed;

For a BCOVPlaybackSession , your code would look similar to this:

 

Setting VR Goggles mode for 360° videos

When playing a 360° video, users can select the Video 360 button on the control bar to switch to VR Goggles mode. You may also want to do this programmatically, before playback starts. You can do this by updating the BCOVPlaybackController protocol's viewProjection property as follows:

 

Start playback when in view

This example will help you to create code to start video playback when the player is in-view and stop playback when it's out-view.

The code below returns a float value for the percentage of the player view that is visible. You can decide when to take action. For example, continue playback when the view is 50% or more visible. Pause playback when the view is under 50% visible. Use an NSTimer or something similar to check the view's visibility over time.

Changing the background color

When playing a video in portrait mode, you may notice a black border on top and below the player. The player view is the size of the screen, but the video only takes up a small part of the center of the player view. The visible portions around the video is the background of the player layer.

This is normal AVPlayer behavior. It shrinks your video to fit inside the player layer, and the rest is the player layer background.

You can change the player layer background with the following code:

 

Setting the background color to white should look like this:

Background color
Custom background color