Skip to main content

SwiftUI Integration

Create a PlayerView

A PlayerView instance is a view integrated with AVPlayer

1. Get otp, playbackInfo & video-id from your backend

Playing vdocipher hosted videos requires these three parameters to authenticate every playback session. Ask the backend to provide the following scheme at the least. The backend application will use this API to get this information.

{
"otp": "____",
"playbackInfo": "____",
"videoId": "_____"
}
Must not be done within app

You must not implement the OTP generating API call from within the application. Making any API call requires the API secret key. Putting this on the app means that anyone can see your keys. Consider your secret API keys like they are passwords.

2. Create an object of VdoViewManager

Create an ObservedObject(below iOS 14)/StateObject(iOS 14 or above) of VdoViewManager using the video-id, otp and playbackInfo

@ObservedObject var viewManager: VdoViewManager = VdoViewManager.init(
videoId: "---",
otp: "---",
playbackInfo: "---")
note

loadError property of VdoViewManager publishes VdoError in case any error occurs while loading the video with given otp and playbackInfo. You must read loadError property to catch this error.

3. Call the initiatePlayback method

Use the VdoViewManager reference generated in Step 2 to play video

viewManager.initiatePlayback { playerView, err in
if let err = err {
self.message = err.message
return
}
guard let pView = playerView else {return}
self.playerView = pView
self.playerView?.player.play()
}

Download handling

VdoViewManager provides simple functions and properties to handle download,

    func initiateDownload()

func deleteDownload()

func cancelDownload()

var downloadState: VdoFramework.VdoAsset.DownloadState?

var percentDownload: Double?

var downloadError: VdoFramework.VdoError?

Sample ContentView File

This sample ContentView.swift file demonstrates all the VdoViewManager APIs available through the SDK. This file can be directly run on a fresh xcode iOS project.

For the AVPlayer container, it uses AVPlayerViewController which will display iOS native player controls.

It contains an offline capable OTP and playbackInfo for demonstration purpose.

import SwiftUI
import VdoFramework

struct ContentView: View {
@ObservedObject var item: VdoViewManager = VdoViewManager.init(
videoId: "0eb55d5dc2264f80b19c0c24bffabfe3",
otp: "20160313versIND313dgKrFOzMsDcQQxb85m3eMUGhATPIqHcGbBkVOmWsaX6jF1",
playbackInfo: "eyJ2aWRlb0lkIjoiMGViNTVkNWRjMjI2NGY4MGIxOWMwYzI0YmZmYWJmZTMifQ==")
@State var message: String = ""
@State var playerView: PlayerView?
var body: some View {
VStack {
playerView
Button("Play") {
item.initiatePlayback { playerView, err in
if let err = err {
self.message = err.message
return
}
guard let pView = playerView else {return}
self.playerView = pView
self.playerView?.player.play()
}
}.buttonStyle(.borderless).padding()
Text(message)
switch item.downloadState {
case .downloaded:
do {
Button("Delete Download") {
item.deleteDownload()
}.buttonStyle(.borderless).padding()
}
case .notDownloaded:
do {
Button("Download") {
item.initiateDownload()
}.buttonStyle(.borderless).padding()
}
case .downloading:
do {
Button("Cancel Download") {
item.cancelDownload()
}.buttonStyle(.borderless).padding()
}
case .none:
do {

}
@unknown default:
fatalError("Failed to identify download state of video \(item.id)")
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}