Skip to main content

Basic Playback

Create the Player

A Player instance could be created by supplying a content URL or an AVURLAsset. The later is preferred for playing protected content, so that license fetching could be configured for the same AVURLAsset.

// Creating Player
if let contentURL = URL(string: playbackAsset.contentUrl) {
let avURLAsset = AVURLAsset(url: contentURL)
let player = FLPlayerFactory.player(asset: avURLAsset)
// add player's playback view to your controller
// player.playbackView
}

Attach Listeners

The application can listen to events such as changes in player state, buffering state, seek state and playback errors by registering a listener/delegate.


extension YourViewController: PlayerDelegate {
func playerStateDidChange(state: PlayerState) {
print("PlayerDelegate: State Change: \(state)")
}

func playerBufferStateDidChange(isBuffering: Bool) {
print("PlayerDelegate: isBuffering: \(isBuffering)")
}

func playerSeekStateDidChange(isSeeking: Bool) {
print("PlayerDelegate: isSeeking: \(isSeeking)")
}

func playerDidFail(with error: FLError) {
print("PlayerDelegate: Player failed with error - \(error)")
}
}

player.delegate = self;

Attach player to a view

The player library could be used with custom player controls and as well with AVPlayerViewController from AVKit.

// Custom Controls
// Attach the player's playback view to your view controller
// Your view controller can have custom player controls
// as per your UX Design requirements
if let playbackView = player.playbackView {
// set the frame of playback view to your view controller view's bounds
playbackView.frame = yourViewController.view.bounds
yourViewController.view.insertSubview(playbackView, at: 0)
}

Using player library with AVPlayerViewController. The player exposes underlying AVPlayer instance through its rawPlayer property. AVPlayerViewController requires this AVPlayer instance for it to work.

// Use Player with AVPlayerViewController
let avPlayerViewController = AVPlayerViewController()
// Access AVPlayer through rawPlayer property on Player and set it on AVPlayerViewController
if let avplayer = player.rawPlayer as? AVPlayer {
avPlayerViewController.player = avplayer
}

Start Playback

The Player library provides load, play, pause and seek APIs. Play API loads the content if not loaded already and plays the content.

player.play()

On both playback stop and abort, player would be paused if playing and all resources would be released and the player would go back to idle state.

note

Always use the control APIs exposed by the FLPlayer library, instead of using the raw player APIs directly.

Stop & Abort

  • Invoking stop(), would stop rendering and all underlying resources would be released.
  • Invoking abort(with error: FLError), would have same effect as stop(). Additionally, playerDidFail(with error: FLError) delegate callback will be invoked.
note

abort(error: Error) will be useful when another component (without user intervention) aborts the playback because of some policy restrictions and the appropriate error has to be shown on the UI.