Using the player

This section gives a brief introduction to the player API in order to help you to get quickly started.

To enable AVPlayer to play audio on physical devices you must add the following code snippet before starting any video/audio playback.

try! AVAudioSession.sharedInstance().setCategory(.playback)

Add the player to UIView

The core of the Flowplayer SDK is the FPFlowplayerViewController which inherits from UIViewController and can therefore be added to a UIView or to a UIViewController in an ordinary fashion like so:

import UIKit
import Flowplayer

class PlayerViewController: UIViewController {
    @IBOutlet var containerView: UIView!

    private let flowplayerViewController = FPFlowplayerViewController()

    override func viewDidAppear(_ animated: Bool) {
        // Add to a container view or directly to your UIViewController's view.
        containerView.addSubview(flowplayerViewController.view)
    }
}

You can also add FPFlowplayerViewController to your Storyboard similar to any other UIViewController.

Prepare player

FPFlowplayerViewController can play media either by fetching a so-called Flowplayer config or directly from a media URL that you specify.

In the first scenario, the player needs to be prepared with a Flowplayer mediaId and a playerId. This is achieved by preparing the player with a FPFlowplayerMedia instance. The player will then fetch the Flowplayer config which contains the media URL, the ad schedule (if any), and branding-related information.

To utilize the Flowplayer Analytics you must use Flowplayer platform mediaIds (either videos hosted on our platform or registered remote assets ) Configuring direct stream urls will not trigger Analytics. You must also load the Analytics plugin.

let fpMedia = FPFlowplayerMedia(mediaId: "some-media-id", playerId: "some-player-id")
flowplayerViewController.prepare(flowplayerMedia: fpMedia, autoStart: true)

In a similar way, the player can be prepared with a local or remote media URL. This is possible by preparing the player with an ExternalVideo instance as shown below. An FPExternalMedia may optionally contain an FPAdSchedule, as well. For more information about ads, see section Advertisement.

let externalMedia = FPExternalMedia(mediaUrl: URL(string: "https://link.to.a.media.file")!)
flowplayerViewController.prepare(externalMedia: externalMedia, autoStart: true)

Limiting video bitrate

When playing HLS media you can configure the preferred maximum peak bitrate.

You can achieve this by defining the preferred peak bitrate as a double when constructing the media:

// Flowplayer media
let fpMedia = FPFlowplayerMedia(
    mediaId: "some-media-id",
    playerId: "some-player-id",
    preferredPeakBitRate: 500_000
)

// ExternalMedia
let externalMedia = FPExternalMedia(
    mediaUrl: URL(string: "https://link.to.a.media.file.hls")!,
    preferredPeakBitRate: 500_000
)

Getting the current playback type

FPFlowplayerViewController contains the property playbackType which is a type of FPPlaybackType.

FPPlaybackType is an enum that contains 4 values:

  • .vod: Video on demand type.
  • .live: Livestream type.
  • .file: File type (e.g. MP4).
  • .unknown: Media type is not known.

This property indicates what type of playback type is currently being displayed by the player.

Note:

Make sure to use this property after the player has loaded the media. If you try to use it before it will return the .unknown type as nothing is being played by the player.

The recommended way is to use it inside of onPlay event.

Background playback

To enable background video playback inside of your app you need to do the following:

  1. Enable Background Modes capability
  2. Connect or Disconnect the Video Player inside of AppDelegate or SceneDelegate
  3. Enable the enableBackgroundPlayback property of FPFlowplayerViewController

Note

For more information please visit Apple's documentation.

Example

// AppDelegate.swift
private var savedPlayerInstance: AVPlayer?

func applicationDidEnterBackground(_ application: UIApplication) {
  let topController = window?.rootViewController as? SomeControllerWhereIUseFlowplayer

  savedPlayerInstance = topController?.videoController.avPlayer

  // Continue playback in the background
  topController?.videoController.avPlayer = nil
}

func applicationWillEnterForeground(_ application: UIApplication) {
  guard savedPlayerInstance != nil else { return }

  // Continue Foreground playback
  let topController = window?.rootViewController as? SomeControllerWhereIUseFlowplayer

  topController?.videoController.avPlayer = self.savedPlayerInstance // Reconnect
  self.savedPlayerInstance = nil
}
// SomeControllerWhereIUseFlowplayer.swift
let videoController = FPFlowplayerViewController()

override func viewDidLoad() {
  super.viewDidLoad()

  videoController.enableBackgroundPlayback = true

}
Results