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)

The FPFlowplayer player

The core of the Flowplayer SDK is the FPFlowplayer a object used to manage: media playback, media assets, playback state and player state.

Think about FPFlowplayer as the manager of your AVPlayer instance that is easy to use, configurable and concise.

Declaration

public class FPFlowplayer {
    public init(player: AVPlayer, controller: UIViewController)
}

To create a FPFlowplayer instance, you need to provide the AVPlayer & the UIViewController that the AVPlayer instance is attached to.

Example

import AVKit
import Flowplayer

class ViewController: UIViewController {

    private var flowplayer: FPFlowplayer!
    private let player = AVPlayer()
    private let playerController = AVPlayerViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        playerController.player = player
        flowplayer = FPFlowplayer(player: player, controller: playerController)
    }
}

FPFlowplayer media types

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

These two media types are:

  1. FPFlowplayerMedia
  2. FPExternalMedia

FPFlowplayerMedia

In the first scenario, the player needs to be loaded with a Flowplayer mediaId and a playerId. This is achieved by providing 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 enable the enableAnalytics flag of FPFlowplayer.

Example

let media = FPFlowplayerMedia(
    mediaId: "some-media-id",
    playerId: "some-player-id"
)

FPExternalMedia

In a similar way, the player can be loaded with a local or remote media URL. This is possible by providing 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 resourceURL = URL(string: "https://link.to.a.media.file")!
let media = FPExternalMedia(mediaUrl: resourceURL)

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
)

Playing media

The method that is used to start media playback is called load, there are two overlays for it (note that the autoStart attribute when set to true will automatically start the media playback as soon as it's ready to be played):

// Flowplayer media
func load(flowplayer media: FPFlowplayerMedia, autoStart: Bool)

// External media
func load(external media: FPExternalMedia, autoStart: Bool)

Use the corresponding one with the desired media type.

Example

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

flowplayer.load(flowplayer: fpMedia, autoStart: true)

OR

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

flowplayer.load(external: externalMedia, autoStart: true)
Results