AVPlayer
You can find a fully functional sample app on our GitHub page: https://github.com/Broadpeak-tv/smartlib-ios-sample-app
1. Attach the player
When creating the player instance, attach it to the current session.
We recommend to init the AVPlayer
instance with the playerWithPlayerItem
method.
// Create your player
AVPlayer *player = [AVPlayer playerWithPlayerItem:nil];
// Attach your player to the session
[session attachPlayer:player];
2. Start a streaming session
- All integrations
- Analytics only integration
The getURL
method is used to get the actual streaming URL and start the streaming session. This method is also the starting point of the analytics calculation, for instance the startup time starts from this method.
The returned URL can be different for every video session. In case of using a Broadpeak CDN or a nanoCDN, it includes a unique token. It can be used only once per session object, once called the session is actually started.
When getURL
returns an error, it means no streaming URL can be found. It has to trigger a specific behaviour on the application (error message, dialog...) and stopStreamingSession
has to be called. Find more details about getURL
errors on the session errors page.
It is recommended to perform this call on a different thread than the main thread, the method runs HTTP requests.
This method has to be called even for non-Broadpeak content URLs. In this case, the method will return the same URL.
// Get the stream URL from SmartLib
StreamingSessionResult *result = [session getURL:...];
if (![result isError]) {
// Load the stream URL into AVPlayer
AVURLAsset *assetUrl = [AVURLAsset assetWithURL:[NSURL URLWithString:[result getURL]]];
AVPlayerItem *itemToPlay = [AVPlayerItem playerItemWithAsset:assetUrl];
[player replaceCurrentItemWithPlayerItem:[self playerItemFromURL:itemToPlay]];
} else {
// Stop the session
[session stopStreamingSession];
// Process the loading error
}
In order to start the analytics session, start the playback on the player:
// Load the stream URL into AVPlayer
AVURLAsset *assetUrl = [AVURLAsset assetWithURL:...];
AVPlayerItem *itemToPlay = [AVPlayerItem playerItemWithAsset:assetUrl];
[player replaceCurrentItemWithPlayerItem:[self playerItemFromURL:itemToPlay]];
[player play];
If you need to reuse the same player instance to start a new session, it is required to stop the playback before.
// Stop the current session
[player replaceCurrentItemWithPlayerItem:nil];
// Start the new session
session = [SmartLib createAnalyticsSession];
[session attachPlayer:player];
[player replaceCurrentItemWithPlayerItem:...];
[player play];
3. Stop a streaming session
- All integrations
- Analytics only integration
The stopStreamingSession
method must be called each time the session is stopped:
- end users: when the playback is stopped by a user action
- loading errors: when the streaming session cannot be started through the
getURL
method - player errors: when the player triggers a non-recoverable error during the playback (as a decoding error)
- application or any others actions: when the app is killed
// Stop the session
[session stopStreamingSession];
[player replaceCurrentItemWithPlayerItem:nil];
If the player generates a non-recoverable error during the playback (as a decoding error), call stopStreamingSession
to stop the session.
// Register to currentItem status events (don't forget to unregister when destroying the player)
[self.player.currentItem addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:PlaybackStatusObservationContext];
// On non-recoverable error, stop the current session
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
AVPlayerItemStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
if (status == AVPlayerStatusFailed) {
[self.session stopStreamingSession];
}
} else {
[super observeValueForKeyPath: keyPath
ofObject: object
change: change
context: context];
}
}
The session report is sent when the player is stopped:
// Stop the session
[player replaceCurrentItemWithPlayerItem:nil];