Using the player
This article is a brief introduction to the player API to help you quickly get started.
info
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 Wowza 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 the player
FPFlowplayerViewController
can play media directly from a media URL that you specify.
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 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:
// 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 four 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.
info
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:
- Enable Background Modes capability.
-
Connect or disconnect the Video Player inside of
AppDelegate
orSceneDelegate
. -
Enable the
enableBackgroundPlayback
property ofFPFlowplayerViewController
.
info
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
}