Bunny Stream is a comprehensive Swift Package Manager (SPM) package designed to seamlessly integrate Bunny's powerful video streaming capabilities into your iOS applications. The package provides a robust set of tools for video management, playback, uploading, and camera-based video uploads, all through an intuitive Swift API.
- Complete API Integration: Full support for Bunny REST Stream API
- Efficient Video Upload: TUS protocol implementation for reliable, resumable uploads
- Advanced Video Player: Custom-built player with full Bunny CDN integration
- Camera Upload Support: Built-in capabilities for recording and uploading videos directly from device camera
- Type-Safe API: Fully typed Swift API for compile-time safety
- Background Processing: Support for background uploads and downloads
- Comprehensive Error Handling: Detailed error information and recovery options
The Bunny Stream package is organized into several specialized packages, each focusing on specific functionality:
Package | Description |
---|---|
BunnyStreamAPI | The core package that provides a comprehensive Swift interface to Bunny's REST Stream API. It handles all API communication, request authentication, and response parsing, allowing you to easily manage your video content, retrieve analytics, and control CDN settings. Features include video management, collection organization, and thumbnail generation. |
BunnyStreamUploader | A sophisticated video upload solution built on the TUS (Tus Upload Server) protocol. This package ensures reliable file uploads even in challenging network conditions, with support for pause/resume functionality, upload progress tracking, and background upload capabilities. It handles chunked uploads, automatic retries, and provides detailed upload status information. |
BunnyStreamPlayer | A feature-rich video player specifically optimized for Bunny's CDN. It provides smooth playback with adaptive bitrate streaming, customizable controls, support for multiple video formats, and integration with Bunny's analytics. The player includes features like airplay support and customizable UI elements. |
BunnyStreamCameraUpload | A comprehensive camera integration solution that enables recording and direct upload of videos from the device camera. It provides easy-to-use APIs for managing video recording, camera controls, and seamless upload integration. |
- iOS 15.0 or later
- macOS 13.0
- Xcode 13.0 or later
Bunny Stream can be integrated into your project using Swift Package Manager (SPM). Here's how to add it to your project:
-
In Xcode, select File > Swift Packages > Add Package Dependency
-
Enter the package repository URL:
https://github.com/BunnyWay/bunny-stream-ios.git
-
Select the version you want to use:
dependencies: [ .package(url: "https://github.com/BunnyWay/bunny-stream-ios.git", .upToNextMajor(from: "1.0.0")) ]
Add the following entries to your Info.plist file:
<!-- For video upload and download -->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<!-- For camera upload -->
<key>NSCameraUsageDescription</key>
<string>Camera access is required for video recording</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone access is required for video recording</string>
<!-- For background upload support -->
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
</array>
After installation, you'll need to configure the package with your Bunny credentials:
import BunnyStreamAPI
// Initialize with your access key
let BunnyStreamAPI = BunnyStreamAPI(accessKey: "your_access_key")
import BunnyStreamAPI
// Initialization
let bunnyStreamAPI = BunnyStreamAPI(accessKey: "your_access_key")
// Example: Get video details
let videoInfo = try await bunnyStreamAPI.client.getVideo(
path: .init(
libraryId: 12345,
videoId: "abcd-e9bb-4b96-wxyz-c17bc6a5292b"
)
)
import BunnyStreamUploader
// Create uploader instance
let videoUploader = TUSVideoUploader.make(accessKey: "your_access_key")
// Prepare video info
VideoInfo(content: .data(video.data),
title: video.name,
fileType: video.type,
videoId: videoId,
libraryId: libraryId)
// Start upload with progress tracking
Task {
do {
try await videoUploader.uploadVideos(with: [videoInfo]) { progress in
print("Upload progress: \(progress.fractionCompleted)")
}
print("Upload completed successfully!")
} catch {
print("Upload error: \(error)")
}
}
For these actions, you'll be using methods from the VideoUploaderActions
protocol.
This ensures that when the background upload completes, or if there's an error, the completionHandler
will be called. This will allow the app to update its UI or notify the user, among other possible actions.
In summary, the TUSVideoUploader
provides an all-in-one solution for robust video uploading, supporting features like pausing, resuming, canceling, and background uploads.
do {
try videoUploader.pauseUpload(for: specificUploadInfo)
try videoUploader.resumeUpload(for: specificUploadInfo)
try videoUploader.removeUpload(for: specificUploadInfo)
} catch {
print("Error performing action: \(error)")
}
When the app goes into the background, especially during an upload, iOS might suspend it after a while. However, with background URL sessions, uploads can continue even when the app is in the background.
In your AppDelegate , when the handleEventsForBackgroundURLSession
method is triggered, you can hand over the background session handling to the videoUploader
.
func application(_ application: UIApplication,
handleEventsForBackgroundURLSession identifier: String,
completionHandler: @escaping () -> Void) {
videoUploader.registerBackgroundHandler(completionHandler, forSession: identifier)
}
import BunnyStreamPlayer
BunnyStreamPlayer(
accessKey: accessKey,
videoId: videoId,
libraryId: libraryId,
cdn: cdnHostname
)
Customizing Player:
You can customize the BunnyStreamPlayer by passing custom icons. Other costumizations like primary color, font, handling control visibilty, captions, heatmap can be controlled from the Bunny dashboard.
extension BunnyStreamPlayer {
static func make(videoId: String) -> BunnyStreamPlayer {
let playerIcons = PlayerIcons(play: Image(systemName: "play.fill"))
return BunnyStreamPlayer(
accessKey: accessKey,
videoId: videoId,
libraryId: libraryId,
cdn: cdnHostname,
playerIcons: playerIcons
)
}
}
// Example view
struct VideoPlayerDemoView: View {
var body: some View {
BunnyStreamPlayer.make(videoId: videoInfo.id)
.navigationBarTitle(Text("Video Player"), displayMode: .inline)
}
}
import BunnyStreamCameraUpload
struct VideoStreamDemoView: View {
@State private var isStreamingPresented = false
var body: some View {
Group { }
.fullScreenCover(isPresented: $isStreamingPresented,
content: {
BunnyStreamCameraUploadView(
accessKey: "<access_key>",
libraryId: <library_id>
)
})
}
}
The package provides detailed error information through custom error types:
do {
let output = try await bunnyStreamAPI.client.deleteVideo(
path: .init(
libraryId: 123,
videoId: "videoId"
)
)
} catch let error as BunnyStreamError {
switch error {
case .unauthorized:
print("Invalid access key")
case .notFound:
print("Video not found")
case .networkError(let underlying):
print("Network error: \(underlying)")
case .apiError(let status, let message):
print("API error (\(status)): \(message)")
}
}
Bunny Stream iOS is licensed under the MIT License. See the LICENSE file for more details.