Concepts
The entry point for the player SDK is the PlayerProtocol
protocol.
This protocol provides functions to control video playback, content, position and rendering.
The player will forward all relevant events to subscribers that were registered with the
addObserver
function. We recommend listening at least to the error event:
class CustomPlayerObserver : PlayerObserver {
func playerError(_ error: Error) {
// handle error
}
}
let player: PlayerProtocol = ...
let observer = CustomPlayerObserver()
player.addObserver(observer) // or more easily, self
We provide different implementations that can be used within your app depending on your needs, but all of them share the same API surface described below.
Player protocol
Playback control
Playback is controlled through simple play
and pause
functions.
// Starts playback of previously added content
func play()
// Pauses playback of previously added content
func pause()
// Calls play if paused, pause if playing
func toggle()
// Like pause(), but the content position is reset to 0
func stop()
Content control
All player implementations can play a single Video
object, a list of Video
s or a Playlist
.
As soon as the content is set through set
(regardless of the play flag), it starts being buffered
and prepared for playback.
// Stops playback and sets new content, which will be buffered.
// If play is true, starts playing as soon as possible.
func set(video: Video, play: Bool)
func set(videos: [Video], at: Int, play: Bool)
func set<P : Playlist>(playlist: P, at: Int, play: Bool)
// Stops playback and clears any content that was previously added
// through `set` functions.
func reset()
Position control
At any given time, you can retrieve the content duration
and the current playback
position
. Buffering will proceed efficiently as the content is played.
We also offer seek*
functions to change the current position.
// The duration of the current content
var duration: TimeInterval { get }
// the current playback position
var position: TimeInterval { get }
// seeks to the given position
func seek(to: TimeInterval, completion: @escaping (Bool) -> Void)
Other options
We also offer a few extra options that can be used to configure the player:
aspectMode
: eitherAspectMode.fill
,AspectMode.crop
orAspectMode.letterbox
.loop
: whether the video should be re-played once it ends.muted
: whether the audio is muted or not.playbackQuality
: either.auto
(recommended) or a fixed value (.fixed(Rendition)
).speed
: a Float controlling the playback speed.
There are also more advanced options which are less frequently used. These can be found by checking the interface ABI.
var muted: Bool { get set }
var loop: Bool { get set }
var aspectMode: AspectMode { get set }
var transition: VideoTransition? { get set }
var playbackQuality: PlaybackQuality { get set }
var speed: Float { get set }