Skip to main content

AVPlayer

note

The QUANTEEC plugin for AVPlayer is currently in a beta state. A stable version is scheduled for the end of May 2024. This documentation might change in the future according to our development.

AVPlayer is the native video player for iOS, created and maintained by Apple.

Compatibility with AVPlayer and QUANTEEC plugin

The QUANTEEC plugin has been tested with AVPlayer from iOS version 9 to iOS version 17. However, for iOS version 11 and lower, the plugin QUANTEEC disable all P2P operations as the technology needed is only supported in iOS version 12 and higher.

Add QUANTEEC plugin as a dependency

Add QUANTEEC plugin module with CocoaPods

The easiest way to start using the QUANTEEC plugin with AVPlayer is to add the plugin as a pod dependency.

First, if your project does not contain a pod file, you can initialize it by using:

pod init

Then, add the following dependency in the pod file of your project:

pod 'QuanteecPlugin', :git==> 'https://github.com/dngroup/quanteeec_ios_pod.git, :branch => 'develop'

To avoid warnings regarding the build version related to the dependency, you can add the following script to your pod file:

post_install do |installer| 
installer.generated_projects.each do |project|
project.targets.each do |target|
target.build.configurations.each do |config|
config.build_settings["IPHONEOS_DEPLOYMENT_TARGET"] = "12.0"
end
end
end
end
note

It's possible to change the value of PHONEOS_DEPLOYMENT_TARGET to 11 or lower, but the WebRTC dependency will trigger a warning.

Then, you can install the dependencies:

pod install

Install QUANTEEC for AVPlayer

4 steps:

1- Import the QUANTEEC plugin:

import QuanteecPlugin

2- Create a QuanteecPlugin plugin configuration and the QuanteecPlugin instance. The constructor of the QuanteecPlugin required the clientID as a parameter:

let quanteecConfig = QuanteecConfig(clientID: "<your-quanteec-key>", videoId: "<your-custom-videoID>")
let quanteecPlugin = QuanteecPlugin(quanteecConfig)
warning

In your QUANTEEC configuration, remember to set a different videoID if you have different formats of the same stream (e.g., one DASH stream and one HLS stream) or two variants of the same stream (one stream with English audio and one stream with another language).

Click here to find more informations on videoID

3- Pass the URL of your stream to the QUANTEEC Plugin instance and indicate a videoID:

let urlString = "<url-of-the-video>"
quanteecPlugin.replaceCurrentItem(with: videoURL, videoID: "<enter-your-custom-videoID>")

4- The quanteecPlugin already embedded an AVPlayer, you need to register to link it to an AVPlayerLayer that will be displayed in your view:

let playerLayer = AVPlayerLayer(player: quanteecPlugin:player)

videoView.layer.addSubLayer(playerLayer)

quanteecPlugin.player.play();

Here is a complete example of how to use the QUANTEEC plugin with AVPlayer inside a Swift View:

import QuanteecPlugin
import AVPlayer

let quanteecPlugin = QuanteecPlugin(clientID: "<your-quanteec-key>")
let urlString = "https://example.com/videoSource.hls"

ovveride func viewDidLoad()
{
super.viewDidLoad();
func loadVideo()
}

func loadVideo() {
let videoURL = URL(string: urlString)
self.quanteecPlugin.replaceCurrentItem(with: videoURL, videoID: "<enter-your-custom-videoID>")

let playerLayer = AVPlayerLayer(player: quanteecPlugin.player)

videoView.layer.addSubLayer(playerLayer)

quanteecPlugin.player.player();
}
tip

Good practice: try-catch block

One good practice is to encapsulate the instantiation of the QUANTEEC object within a try-catch block. This approach provides a robust mechanism for handling situations where QUANTEEC might not be available or may encounter compatibility issues.

Error handling

Embedding QUANTEEC in an iOS app involves configuring and initializing the QUANTEEC plugin instance. If any issues arise during this process, such as missing dependencies or runtime errors, a try-catch block allows you to catch and handle these errors gracefully.

Unusual environments

iOS devices and versions vary, and not all devices may support certain features or configurations of QUANTEEC. Wrapping the QUANTEEC initialization in a try-catch block allows you to account for scenarios where certain features might not be available on the user's device or where there are compatibility issues with the iOS version.