Video player SDK for iOS
The SDK
To use the SDK, you need the Xcode
Adding a video player SDK library
-
In the Project Navigator Xcode window, select your project.
-
In the top panel, click File and select Add Package Dependencies....
-
In the search box
, enterhttps://github.com/yandex-cloud/cloud-video-player-ios-sdk/. Then, select thecloud-video-player-ios-sdkpackage. -
In the Dependency Rule field, select Up to Next Major Version and specify the version:
0.1.6. -
In the Add to Project field, select the project you want to add libraries to and click Add Package.
-
In the pop-up window, specify the target to add the libraries to and click Add Package.
The package contains the following libraries:
CloudVideoPlayer: Main library of the video player SDK for iOS.CloudVideoPlayerUI: Additional library with a collection of interface elements (video player skin).
-
In the Project Navigator Xcode window, select your project.
-
Open
Package.swift. -
Add the following dependency to the
dependenciesarray:dependencies: [ .package( url: "https://github.com/yandex-cloud/cloud-video-player-ios-sdk/", from: "0.1.6" ) ], -
Add the libraries to the
dependenciesarray of the target:.target( name: "MyTargetName", dependencies: [ .product(name: "CloudVideoPlayer", package: "cloud-video-player-ios-sdk"), .product(name: "CloudVideoPlayerUI", package: "cloud-video-player-ios-sdk") ] ),Where:
CloudVideoPlayer: Main library of the video player SDK for iOS.CloudVideoPlayerUI: Additional library with a collection of interface elements (video player skin).
-
Save your changes.
Importing the libraries
To import the libraries, add the following lines to your code file:
import CloudVideoPlayer
import CloudVideoPlayerUI
Using the SDK
Set up the start of playback
-
Import a library in the file:
import CloudVideoPlayer -
Create the
Configuration,Environment, andYaPlayerobjects:let environment = Environment(configuration: Configuration(from: From(raw: "your-app-bundle"))) class ViewController: UIViewController { let player = environment.player() } -
Create the
VideoSurfaceUIView component, add it to the hierarchy, and attach to the player instance:let surface = VideoSurface() override func loadView() { super.loadView() self.view.addSubview(surface) surface.frame = UIScreen.main.bounds } override func viewDidLoad() { super.viewDidLoad() surface.attach(player: player) } -
Start playback:
if let source = ContentIdEndpoint(url: URL(string: "https://runtime.video.cloud.yandex.net/player/...")!) { player.set(source: source) player.play() }
Where https://runtime.video.cloud.yandex.net/player/... is a link to a video, broadcast, or playlist.
Full code to set up the start of playback
import CloudVideoPlayer
let environment = Environment(configuration: Configuration(from: From(raw: "your-app-bundle")))
class ViewController: UIViewController {
let player = environment.player()
let surface = VideoSurface()
override func loadView() {
super.loadView()
self.view.addSubview(surface)
surface.frame = UIScreen.main.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
surface.attach(player: player)
if let source = ContentIdEndpoint(url: URL(string: "https://runtime.video.cloud.yandex.net/player/...")!) {
player.set(source: source)
player.play()
}
}
}
Where https://runtime.video.cloud.yandex.net/player/... is a link to a video, broadcast, or playlist.
Connecting a video player skin
-
Import a library in the file:
import CloudVideoPlayerUI -
Create the
VideoViewUIView component, add it to the hierarchy, and connect to the player instance:let videoView = VideoView() override func loadView() { super.loadView() self.view.addSubview(videoView) videoView.frame = UIScreen.main.bounds } override func viewDidLoad() { super.viewDidLoad() videoView.attach(player: player) }
Full code to connect a video player skin
import CloudVideoPlayerUI
let videoView = VideoView()
override func loadView() {
super.loadView()
self.view.addSubview(videoView)
videoView.frame = UIScreen.main.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
videoView.attach(player: player)
}
Playback in SwiftUI
To embed your player in SwiftUI, wrap VideoView from CloudVideoPlayerUI into UIViewRepresentable, then bind it to the YaPlayer instance created via Environment.
-
Import these libraries in the file:
import SwiftUI import CloudVideoPlayer import CloudVideoPlayerUI -
Create the
EnvironmentandYaPlayerobjects:let environment = Environment(configuration: Configuration(from: From(raw: "your-app-bundle"))) final class PlayerViewModel: ObservableObject { let player: YaPlayer = environment.player() init() { guard let url = URL(string: "https://runtime.video.cloud.yandex.net/player/..."), let source = ContentIdEndpoint(url: url) else { return } player.set(source: source) player.play() } } -
Create the
UIViewRepresentabletype that creates yourVideoViewand callsattach(player:):struct VideoViewRepresentable: UIViewRepresentable { let player: YaPlayer func makeUIView(context: Context) -> VideoView { let view = VideoView() view.attach(player: player) return view } func updateUIView(_ uiView: VideoView, context: Context) { uiView.attach(player: player) } } -
Add the wrapper to the SwiftUI hierarchy:
struct ContentView: View { @StateObject private var viewModel = PlayerViewModel() var body: some View { VideoViewRepresentable(player: viewModel.player) .aspectRatio(16 / 9, contentMode: .fit) .frame(maxHeight: .infinity, alignment: .top) } }
Where https://runtime.video.cloud.yandex.net/player/... is a link to a video, broadcast, or playlist.
Full code for playback in SwiftUI
import SwiftUI
import CloudVideoPlayer
import CloudVideoPlayerUI
let environment = Environment(configuration: Configuration(from: From(raw: "your-app-bundle")))
final class PlayerViewModel: ObservableObject {
let player: YaPlayer = environment.player()
init() {
guard
let url = URL(string: "https://runtime.video.cloud.yandex.net/player/..."),
let source = ContentIdEndpoint(url: url)
else {
return
}
player.set(source: source)
player.play()
}
}
struct VideoViewRepresentable: UIViewRepresentable {
let player: YaPlayer
func makeUIView(context: Context) -> VideoView {
let view = VideoView()
view.attach(player: player)
return view
}
func updateUIView(_ uiView: VideoView, context: Context) {
uiView.attach(player: player)
}
}
struct ContentView: View {
@StateObject private var viewModel = PlayerViewModel()
var body: some View {
VideoViewRepresentable(player: viewModel.player)
.aspectRatio(16 / 9, contentMode: .fit)
.frame(maxHeight: .infinity, alignment: .top)
}
}
Where https://runtime.video.cloud.yandex.net/player/... is a link to a video, broadcast, or playlist.
See also
SDK library references:
- CloudVideoPlayer: Main library with
Environment,YaPlayer, andVideoSurfaceobjects and playback settings. - CloudVideoPlayerUI: Additional UI component library with a ready-made
VideoViewplayer shell.