In the Playback
section we list the common features shared by all player engines (PlayerEngine.apple
)
Basic playback
An example project of basic HLS playback is provided as part of the PRESTOplay SDK. The example is located in the Examples
folder and can be opened with Xcode.
import PRESTOplay
import CastlabsApple
// Initialize the SDK and register the plugins
_ = PRESTOplaySDK.shared.register("LICENSE", [HLSPlugin()])
// Create the player
var player = PRESTOplaySDK.shared.player()
guard let contentURL = URL(string: "https://example.com/master.m3u8")
else { return }
// Configure the player
let config = PlayerConfiguration(with: contentURL)
player.load(config: config)
player.onState = { previous, state in
// Handle player state changes
switch state {
case .ready:
print("Player ready")
// handle other states ...
}
if let error = state.playerError {
print("Error \(error)")
}
}
// Attach the player to the view
player.attach(to: view.layer)
// Start playback
player.open(autoplay: true)
Video gravity
Video gravity determines how the video content is scaled or stretched within the player bounds.
The player layer supports the following video gravity values:
AVLayerVideoGravity.resizeAspect
player should preserve the video’s aspect ratio and fit the video within the bounds,
AVLayerVideoGravity.resizeAspectFill
player should preserve the video’s aspect ratio and fill the bounds,
AVLayerVideoGravity.resize
video should be stretched to fill the bounds
Phone call
By default the player is paused when a phone call has started and resumes playback when a phone call ends.
Airplay
Airplay is supported and no changes are required from the SDK user. Specifically:
- HLS streams are natively supported, i.e. you only need to switch on Airplay on your device
- MPEG-DASH requires to switch on Airplay and to active the screen mirroring
By default, AirPlay is disabled for DRM-encrypted content. To enable, set preventSecondScreenPlayback to false in the DRM configuration.
playerConfiguration.drmConfiguration?.preventSecondScreenPlayback = false
If the content requires an authentication token (playerConfiguration.drmConfiguration.authToken), implement the PRESTOplaySDK.shared.onAirplayAuthTokenRequired()
method to provide authentication key for AirPlay. For example,
PRESTOplaySDK.shared.onAirplayAuthTokenRequired = { [weak self] playerConfiguration in
guard let self,
let configurationUrl = playerConfiguration.configurationUrl else {
return nil
}
var authToken: String?
let group = DispatchGroup()
group.enter()
Utils.loadAuthToken(from: configurationUrl, userToken: self.userToken) { token in
authToken = token
group.leave()
}
let _ = group.wait(timeout: .now() + 10)
return authToken
}
In the example code, the userToken
is a key obtained from your DRM provider.
Player statistics
The Player API offers two main methods for accessing statistics data: onStats
callback for real-time monitoring, ideal for logging QoE or other stats, and getStats
method to fetch the latest stats programmatically. If stats aren’t available, nil
is returned.
import PRESTOplay
import CastlabsApple
// Initialize the SDK and register the plugins
_ = PRESTOplaySDK.shared.register("LICENSE", [HLSPlugin()])
// Create the player
var player = PRESTOplaySDK.shared.player()
guard let contentURL = URL(string: "https://example.com/master.m3u8")
else { return }
// Configure the player
let config = PlayerConfiguration(with: contentURL)
player.load(config: config)
// ...
// Observe stats
player.onStats = { stats in
print("Stats: \(stats)")
}
// Get the latest reported stats
let stats = player.getStats()
print("Stats: \(stats)")
// ...