AVPlayer integration
Integrate AVPlayer
Installing MLYSDK
- Installing with CocoaPods
To install with CocoaPods, modify your Podfile to use frameworks by including use_frameworks! and then add the following pods to your Podfile:
pod 'MLYSDK',:git => 'https://github.com/mlytics/mly-stream-sdk-ios-avplayer.git'
- Installing with SwiftPM
- In XCode click "File" > "Swift Packages" > "Add Package Dependency..."
- The package repository URL is https://github.com/mlytics/mly-stream-sdk-ios-avplayer.git
- Click next.
Initialize the driver for your AVPlayer instance
Get your client_id
from the stream Code Snippet.
To initialize SDK, we need to call MLYDriver.initialize
first. Here's an example showing how you could initialize SDK.
do {
try MLYDriver.initialize { options in
options.client.id = "{CLIENT_ID}"
}
} catch {
print(error)
}
[MLYDriver initializeAndReturnError: &error :^(MLYDriverOptions * option) {
[[option client] setId:@"{CLIENT_ID}"];
}];
Setup your AVPlayer And AVPlayerViewController
Create an AVPlayer instance and add it to your view controller's view.
var player = AVPlayer()
var playerViewController = {
let controller = AVPlayerViewController()
controller.showsPlaybackControls = true
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.view.backgroundColor = .darkGray
controller.player = player
return controller
}()
AVPlayer player = [[AVPlayer alloc] init];
AVPlayerViewController playerController = [AVPlayerViewController new];
playerController.showsPlaybackControls = true;
playerController.view.backgroundColor = UIColor.darkGrayColor;
playerController.player = player;
Configure SDK Adapter
In order to use SDK to download the video, we need to configure the SDK Adapter by passing your PlayerViewController
instance. Here's an example showing how you could configure SDK Adapter.
var plugin = MLYAVPlayerPlugin()
plugin.adapt(playerViewController)
MLYAVPlayerPlugin plugin = [MLYAVPlayerPlugin alloc];
[plugin adapt:playerController];
Start playing the media
To play the media, call the play() method on the AVPlayer instance.
let url = URL(string: "https://your.video.website.com/your_video.m3u8")!
let playerItem = AVPlayerItem(url: url)
player.replaceCurrentItem(with: playerItem)
player.play()
NSURL *url = [NSURL URLWithString:@"https://your.video.website.com/your_video.m3u8"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
[player replaceCurrentItemWithPlayerItem:item];
[player play];
Full example
Updated 5 months ago