AVPlayer
AVPlayer is the native video player for iOS/iPadOS/tvOS, created and maintained by Apple.
Compatibility with AVPlayer and QUANTEEC plugin
The QUANTEEC plugin has been tested with AVPlayer from iOS/iPadOS/tvOS version 15.6 to iOS version 18.
Add QUANTEEC plugin as a package (using Swift Package Manager)
The QUANTEEC plugin is available as a package that can be added to your project using the Swift Package Manager. To add the QUANTEEC plugin to your project, follow these steps:
- Open your Xcode project.
- Select your project in the Project Navigator.
- Select the target you want to add the package to.
- Go to the "Package Dependencies" tab.
- Click the "+" button to add a new package.
- Enter the URL of the QUANTEEC plugin repository:
https://github.com/dngroup/quanteec_ios_package
. You need to have linked your github account to your Xcode account. - The packages "quanteec_plugin_package" and the dependency "GDCWebServer" should appear on the left panel under "package dependencies".
Install QUANTEEC in your application for AVPlayer
3 steps:
1- Import the QUANTEEC plugin:
import QuanteecPlugin
2- Create a QuanteecPlugin plugin configuration (QuanteecConfig). The constructor of the QuanteecPlugin required the quanteecKey as a parameter:
let quanteecConfig = QuanteecConfig(quanteecKey: "<your-quanteec-key>")
quanteecConfig.videoID = "<your-custom-videoID>"
To create a new QUANTEEC configuration and/or retrieve your default QUANTEEC key, you must first go to the Config page of your panel.
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).
3- Create a QuanteeciOSPlugin instance. The constructor of the QuanteecPlugin required the quanteecConfig and the AVPlayer instance as parameters:
let quanteecPlugin = QuanteeciOSPlugin(quanteecConfig: quanteecConfig, player: player)
Here is a complete example of how to use the QUANTEEC plugin with AVPlayer inside a basic Swift View component:
import SwiftUI
import AVKit
import QuanteecPlugin
struct ContentView: View {
let player = AVPlayer()
let urlString: String = "https://example.com/videoSource.m3u8"
@State private var quanteecPlugin: QuanteeciOSPlugin?
@State private var dataCDN: String = "0"
@State private var dataPeer: String = "0"
@State private var dataSent: String = "0"
@State private var timer: Timer? = nil
var body: some View {
VStack {
VideoPlayer(player: player)
.frame(height: 300)
.onAppear {
if let videoURL = URL(string: urlString) {
let playerItem = AVPlayerItem(url: videoURL)
player.replaceCurrentItem(with: playerItem)
player.play()
startUpdatingStats()
}
}
.onDisappear {
player.pause()
stopUpdatingStats()
}
// Statistics display
VStack(alignment: .leading, spacing: 8) {
Text("Statistics:")
.font(.headline)
HStack {
Text("CDN Data:")
Spacer()
Text(dataCDN)
}
HStack {
Text("Peer Data:")
Spacer()
Text(dataPeer)
}
HStack {
Text("Data Sent:")
Spacer()
Text(dataSent)
}
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
.padding(.horizontal)
}
.padding()
.task {
do {
let quanteecConfig = QuanteecConfig(quanteecKey: "<your-quanteec-key>")
quanteecConfig.videoID = "<your-custom-videoID>"
quanteecPlugin = QuanteeciOSPlugin(quanteecConfig: quanteecConfig, player: player)
}
}
}
func startUpdatingStats() {
updateStats()
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
updateStats()
}
}
func updateStats() {
dataCDN = formatBytes(quanteecPlugin?.getDataCDN())
dataPeer = formatBytes(quanteecPlugin?.getDataPeer())
dataSent = formatBytes(quanteecPlugin?.getDataSent())
}
func stopUpdatingStats() {
timer?.invalidate()
timer = nil
}
func formatBytes(_ bytes: Int64?) -> String {
guard let bytes = bytes else { return "0 B" }
if bytes == 0 {
return "0 B"
}
let formatter = ByteCountFormatter()
formatter.allowedUnits = [.useKB, .useMB, .useGB]
formatter.countStyle = .file
return formatter.string(fromByteCount: Int64(bytes))
}
}
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.