Getting started¶
This guide walks you through adding PRESTOplay for Apple to your project, initializing the SDK, and playing your first stream.
Prerequisites¶
Xcode
A Castlabs license key (create one at https://downloads.castlabs.com)
Add the SDK to your project¶
The SDK is available via Swift Package Manager (SPM), CocoaPods, or manual XCFramework integration.
Swift Package Manager¶
Add the PRESTOplay package repository to your Xcode project:
In Xcode, select File > Add Package Dependencies
Enter the Castlabs SPM repository URL
Select the packages you need (at minimum
PRESTOplayandCastlabsApplefor HLS playback)
CocoaPods¶
Add the Castlabs pod source and dependencies to your Podfile:
source 'https://github.com/castlabs/client_podspecs.git'
use_frameworks!
pod 'PRESTOplay', '<version>'
pod 'CastlabsApple', '<version>'
Then run pod install.
Manual integration¶
Unzip the SDK bundle
Add
PRESTOplay.xcframeworkto your project under Frameworks, Libraries, and Embedded Content with Embed & SignRepeat for any additional plugin frameworks (e.g.,
CastlabsApple.xcframework)
Initialize the SDK¶
Call PRESTOplaySDK.shared.setup() once during app startup, passing your
license key and the plugins you want to activate. For HLS playback, you need at
minimum the HLSPlugin:
import PRESTOplay
import CastlabsApple
let error = PRESTOplaySDK.shared.setup("your-license-key", [HLSPlugin()])
if let error = error {
print("SDK initialization failed: \(error)")
}
You’re also responsible for configuring the audio session. If your app only does
playback, AVAudioSessionCategory.playback is sufficient:
import AVFoundation
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playback)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch {
print("Cannot initialize audio session")
}
Set up error handling¶
Player operations happen asynchronously, so errors are reported through a global callback. Attach your handler before opening any content:
PRESTOplaySDK.shared.onError = { component, error in
print("Error from \(component): \(error)")
}
Start playback¶
Create a player, configure it with a stream URL, attach it to a view layer, and open the content:
import PRESTOplay
import CastlabsApple
// Create the player
var player = PRESTOplaySDK.shared.player()
guard let contentURL = URL(string: "https://demo.cf.castlabs.com/media/prestohls/master.m3u8")
else { return }
// Configure and load the player
let config = PlayerConfiguration(with: contentURL)
player.load(config: config)
// Handle state changes
player.onState = { previous, state in
switch state {
case .ready:
print("Player ready")
case .playing:
print("Playing")
default:
break
}
if let error = state.playerError {
print("Error: \(error)")
}
}
// Attach the player to a view layer
player.attach(to: view.layer)
// Start playback
player.open(autoplay: true)
License user identification¶
If your license requires a user ID, set it on the PlayerConfiguration before
loading:
let config = PlayerConfiguration(with: contentURL)
config.userId = "your-user-id"
player.load(config: config)