FastPix player for iOS
The FastPix iOS Player SDK makes it easy to add video playback to your iOS app. Whether you're streaming live events or on-demand content, it’s built to handle both with smooth performance. It’s designed specifically for FastPix video streams and integrates cleanly into native workflows, ensuring a smooth and dynamic video playback experience.
Installing the SDK in your iOS project
This guide will help you install the FastPix iOS Player SDK in your native iOS application. We'll use the Swift Package Manager to install the FastPix iOS Player SDK. Here is Step-by-step guide on using Swift Package Manager in Xcode.
To add the FastPix iOS Player SDK into your project in Xcode, follow these steps:
- Open your project in Xcode.
- In the Xcode menu bar, go to File > Add Packages. This will bring up a new window.
- In the top-right corner of this window, enter the SDK repository URL:
By default, Xcode will pull the latest version of the SDK from the main branch. If you’d like to specify a particular version or set a range of compatible versions, you can adjust the Dependency Rule settings. Different Dependency Rules let you control how updates to the SDK are managed for your project.
Importing FastPix iOS player SDK in your iOS project
After successfully installing the FastPix iOS Player SDK, the next step is to make it accessible within your project by importing it into your Swift files.
Step 1: Open your swift file
Navigate to the Swift file where you want to use the FastPix iOS Player SDK. This could be a file like ViewController.swift
or any other file where you'll implement video playback features.
Step 2: Add the import statement
At the very top of the file, include the following line of code:
// import fastpix ios player sdk into swift files
import FastPixPlayerSDK
This import statement ensures that all the classes, methods, and functionalities provided by the FastPix iOS Player SDK are available for use in your project.
Step 3: Start integrating player features
Once the SDK is imported, you can begin working with its APIs. This includes initializing the player, configuring playback options, and handling video streams.
By completing this step, you’re ready to leverage the capabilities of the FastPix iOS Player SDK and bring high-quality video streaming to your application.
Play videos with the FastPix iOS player
Use FastPixPlayerSDK to configure an AVPlayerController or AVPlayerLayer for streaming a FastPix asset using just the playback ID that was generated for the video that you uploaded to FastPix.
import UIKit
import FastPixPlayerSDK
import AVKit
class VideoPlayerViewController: UIViewController {
// Playback ID and Token for streaming (to be set dynamically)
var playbackID = ""
var playbackToken = ""
// Lazy initialization of AVPlayerViewController
lazy var playerViewController = AVPlayerViewController()
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Prepare and attach the AVPlayerViewController to the view
prepareAvPlayerController()
// Start playback
playerViewController.player?.play()
}
// MARK: - AVPlayer Setup
/// Sets up the AVPlayerViewController and attaches it to the view hierarchy
func prepareAvPlayerController() {
// Add playerViewController as a child
playerViewController.willMove(toParent: self)
addChild(playerViewController)
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
// Disable autoresizing mask constraints
playerViewController.view.translatesAutoresizingMaskIntoConstraints = false
// Constrain player view to safe area
NSLayoutConstraint.activate([
playerViewController.view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
playerViewController.view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
playerViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
playerViewController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
}
}
Key features of FastPix iOS player
The FastPix iOS Player is built to integrate seamlessly into your iOS apps and deliver smooth, high-quality video playback, whether it's live or on-demand. With minimal setup, you can get started quickly.
Media playback
FastPix makes it easy to stream public videos. The player supports both live and on-demand content and provides all the functionality required for reliable, high-performance playback.
To play a video, you typically need two parameters:
streamType
This parameter tells the player what kind of content to expect: on-demand or live. It controls how the stream is handled. If not specified, streamType defaults to on-demand.
playbackId
This is a unique identifier for the video asset. It's required for streaming public media and ensures the correct video is loaded. Make sure to set the right playbackId to avoid errors.
// create an object of AVPlayerViewController()
lazy var playerViewController = AVPlayerViewController()
// using prepare method to play the videos using playbackID
playerViewController.prepare( playbackID: playbackID )
Note :
If the streamType is not specified then it is by default taken as on-demand.
Set the correct stream type for your video
In the FastPix iOS Player, the streamType parameter defines the type of media being played and how the player handles it. This allows you to optimize playback for different types of content, such as live streams or on-demand videos.
Supported stream types
- on-demand: For pre-recorded, non-live content.
- live: For real-time streaming content.
Specify the appropriate stream type based on the media being played to ensure optimal playback behavior.
Playing on-demand videos with FastPix iOS player
To play on-demand
videos using the FastPix iOS Player, you need to specify the streamType as on-demand and provide the necessary identifiers like the playbackId. Follow the steps below to configure the player for on-demand playback:
Steps:
- Set the Stream Type: Ensure the streamType is set to on-demand. If the streamType is not specified by default the stream is on-demand.
- Provide the playbackId: The playbackId is a unique identifier for the on-demand video content.
// using prepare method to play the videos using playbackID and stream type here the stream type is set to on-demand
playerViewController.prepare(playbackID: playbackID,playbackOptions: PlaybackOptions(streamType: "on-demand"))
Playing live videos with FastPix iOS player
To play live videos using the FastPix iOS Player, you need to specify the streamType as live and provide the necessary identifiers like the playbackId for the live stream. Follow the steps below to configure the player for live playback:
Steps:
- Set the Stream Type: Ensure the streamType is set to live.
- Provide the playbackId: The playbackID is a unique identifier for the live stream content.
- Configure the Player: Pass the configuration to the player and start the live stream.
// use the prepare method to play the videos using playbackID and stream type here the stream type is set to live
playerViewController.prepare(playbackID: playbackID,playbackOptions: PlaybackOptions(streamType: "live"))
Secured media playback in FastPix iOS player
To protect private media content in your iOS application using the FastPix iOS Player, it’s crucial to implement security measures that control access and ensure data integrity. Below are the best practices for securing private media streams.
Security practices
- Private media should be accessed using dynamically generated, secure playback IDs. These IDs should be short-lived and tied to the user's authenticated session to prevent unauthorized access.
- Ensure that the user is authenticated (e.g., using OAuth, JWT) before granting access to private media. Only allow users with the proper permissions to access the media.
- Always use HTTPS for API calls and media streams to prevent the interception of sensitive data during transmission.
- Secure media access by generating signed tokens for playback IDs, which are valid only for a limited time and associated with a specific user session.
- For an additional layer of security, encrypt the media files before streaming. This ensures that even if the stream is intercepted, unauthorized users cannot access the content without the decryption key.
Securing on-demand streams
To ensure that your on-demand streams are securely accessed, FastPix allows you to implement access tokens to secure playback. These tokens act as a safeguard to ensure that only authorized users can view the content, and they are typically tied to user sessions, valid for a limited time, and expire once they’re no longer needed.
How access tokens work
Access tokens are short-lived credentials that authenticate the user’s right to view the content. When a user attempts to access an on-demand stream, the player uses the token to verify that the user is authorized to view the media.
Here’s a breakdown of how you can secure your on-demand streams using access tokens:
- Token generation:
- Access tokens are typically generated on the server-side after the user has been authenticated.
- The token will contain a signature and expiration date, ensuring it is valid for a limited period (e.g., 1 hour).
- Here’s a guide on how to generate access tokens: https://docs.fastpix.io/docs/authentication-with-access-tokens
- Token integration:
- When configuring the FastPix iOS Player for on-demand playback, you’ll include the access token as part of the media request.
- The access token is passed along with the playbackId, ensuring that only users with a valid token can access the stream.
How to generate JWT Access Token from FastPix
To enable secure video playback, you need to provide a valid JWT access token. This token helps protect your content by authorizing only permitted viewers.
For a detailed guide on how to generate a JWT token, refer to the official documentation:
https://docs.fastpix.io/docs/secure-playback-with-jwts
// use this method to play the on-demand videos securely with token
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions(playbackToken: playbackToken))
Securing live streams
When streaming live content through your iOS app, it's essential to ensure that the stream is protected from unauthorized access and potential tampering. Here’s a guide on securing live streams in your iOS player:
Token-based authentication for access control
- Token-based authentication ensures that only authorized users can access your live streams.
- Generate a unique token for each user or session. This token can be included in the URL or in the headers of the request.
- The server generates a signed URL with a token (JWT or similar) for each user session.
- Pass this signed URL to the player when loading the stream
- The server validates the token before allowing access to the stream.
- You can generate the playbackToken directly through FastPix. Refer to the detailed guide on generating access tokens provided by FastPix for step-by-step instructions https://docs.fastpix.io/docs/authentication-with-access-tokens
// use this method to play the live streams securely with token
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( streamType: “live”,playbackToken: playbackToken))
Possible errors when playing secured stream content
When playing secured streams, several issues can cause playback failures. Below are some of the possible errors you may encounter:
Expired or invalid authentication token
- If the authentication token used for securing the stream has expired or is invalid, the player will not be able to access the content.
- Ensure the token is valid and has not expired before starting playback.
Network connectivity issues
- Unstable or slow network connections can cause interruptions or failures in stream playback.
- Ensure the network connection is stable and retry the connection if necessary.
Corrupted or unsupported stream format
- The stream may be corrupted or in a format that is not compatible with the player (e.g., an unsupported HLS version or missing media segments).
- Verify that the stream is in a supported format and is being served correctly without corruption.
Playing media with custom domain
- With FastPix iOS Player, you can stream videos from your custom domain without even specifying stream types, tokens, or playback configurations. Simply provide your custom domain, and the player will handle the playback offering an easy and efficient way to integrate video content into your iOS application.
- To play videos from a custom domain, you need to configure the player to stream content from your own video server or any hosting server. You need to set up the player for playback of videos hosted on your custom domain.
Specifying custom domain for FastPix iOS Player
Specifying a custom domain in the FastPix iOS Player lets you play videos directly from your own domain, ensuring smooth playback without requiring extra configurations.
// use this method to play with customDomain and playbackID
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions(customDomain: "customeDomain"))
// use this method to play with custom Domain, playbackID and playback token
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions(customDomain: "customeDomain", playbackToken: playbackToken))
Specifying custom domain for live streams
The FastPix iOS Player supports live streaming directly from your custom domain, allowing you to stream live content ensuring uninterrupted playback.
// use this method to play with custom Domain, playbackID, playback token and streamType as live
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions(customDomain: "customeDomain", playbackToken: playbackToken, streamType: “live”))
Audio track switching
The FastPix iOS Player offers support for handling multiple audio tracks, enabling users to switch between different audio options during video playback. This is particularly useful for multi-language content or media with additional audio tracks.
Here’s how this feature enhances the video playback experience:
- Dynamic detection: FastPix Player dynamically identifies all available audio tracks embedded in the video playback.
- User-controlled switching: Developers can provide users with an interface to switch audio tracks during playback without restarting the stream.
- Auto selection: The player selects the default audio track based on metadata or user preferences.
For example, in a live stream scenario with multiple language options, users can effortlessly switch between English, Spanish, or other supported audio tracks with minimal latency.
Control video playback resolution
The FastPix iOS Player provides flexibility to customize the video playback resolution to meet your specific requirements. This feature allows developers to offer users the ability to choose from different video resolutions. Here’s how you can use this feature:
- The player automatically detects and offers available resolution options for the video stream. It ensures that users can select the best resolution based on their preferences, whether they want higher video quality or faster streaming.
- If you want to provide a configured resolution for playback, you can configure specific resolution settings within the player. This can be useful for scenarios where you want to ensure videos play at a certain resolution based on user context, such as device type, network speed, or specific requirements for your application.
Benefits
Custom resolution support allows users to have more control over their viewing experience, reducing buffering and ensuring smooth playback.
By adjusting resolution based on network conditions, FastPix iOS Player minimizes data usage, helping users on limited data plans or slower network connections.
To integrate this functionality, simply provide the customize resolutions in playback options in FastPixiOSPlayer.
Configuring minimum resolution
FastPix iOS Player allows developers to set a minimum resolution for video playback. This feature ensures that the player only streams videos at or above the specified resolution.
// use this method to play videos with custom min resolution and playbackID
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) ))
// use this method to play videos with custom min resolution,playbackID and playbackToken
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) , playbackToken: playbackToken ))
Configuring maximum resolution
With FastPix iOS Player you can set a maximum resolution for video playback. This feature ensures that the player only streams videos upTo or less than the specified resolution.
// use this method to play videos with custom max resolution and playbackID
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( maxResolution : (example : .upTo1080p) ))
// use this method to play videos with custom max resolution,playbackID and playbackToken
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( maxResolution : (example : .upTo1080p) , playbackToken: playbackToken ))
Setting a specific resolution for video playback
Using FastPix iOS Player, you can define a specific resolution for video playback. This allows you to set a particular resolution, ensuring that the player will stream videos only at that given specific resolution.
// use this method to play videos with a specific resolution,playbackID
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( resolution : (example : .set480p)))
// use this method to play videos with a specific resolution,playbackID and playbackToken
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( resolution : (example : .set480p, playbackToken: playbackToken )))
Combination of min and max resolution
FastPix iOS Player allows users to define both a minimum and maximum resolution for video playback. This ensures that the player streams videos within the specified resolution range, providing optimal playback quality based on the user's settings.
// use this method to play videos with in a specific range of resolution and playbackID
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) , maxResolution : (example : .upTo1080p)))
// use this method to play videos with in a specific range of resolution playbackID and playbackToken
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) , maxResolution : (example : .upTo1080p , playbackToken: playbackToken)))
Specifying rendition order for combination of min and max resolution
With FastPix iOS Player, you can specify the rendition order for a combination of minimum and maximum resolutions. This allows you to control the sequence in which video quality levels are selected, ensuring that the player prioritizes the most appropriate resolution based on the defined range.
By default, the FastPix iOS Player sets the rendition order to ascending. This means that the video player will start by selecting the lowest available resolution within the specified range and gradually increase the resolution as the playback progresses, depending on network conditions and performance requirements.
// use this method to play videos with in a specific range of resolution playbackID and renditionOrder
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) , maxResolution : (example : .upTo1080p , renditionOrder: .descending ))
// use this method to play videos with in a specific range of resolution playbackID ,renditionOrder and playbackToken
playerViewController.prepare(playbackID: playbackID, playbackOptions: PlaybackOptions( minResolution : (example : . atLeast270p) , maxResolution : (example : .upTo1080p , renditionOrder: .default, playbackToken: playbackToken )))
This guide provides everything you need to get started with the FastPix iOS Player, enabling video playback with all the features outlined above for a smooth and uninterrupted experience in your iOS applications.
Updated 1 day ago