Using the player

This section is a brief introduction to the player API to help you get started quickly.

info

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

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

The FPFlowplayer player

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

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

Declaration

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

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

Example

Copy
Copied
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 directly from a media URL that you specify. The media type is FPExternalMedia.

FPExternalMedia

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 Advertisement.

Copy
Copied
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:

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

Playing media

The method that's 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:

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

Example

Copy
Copied
// 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)