In the vibrant realm of iOS development, the ability to seamlessly integrate audio and video playback can significantly enhance the user experience. The AVPlayer class, intrinsic to Apple’s AVFoundation framework, provides developers with a robust toolset for controlling and managing this multimedia content.
Whether you’re looking to embed videos, stream music, or even delve into the intricacies of Digital Rights Management (DRM) and Adaptive Bitrate Streaming, AVPlayer stands as the heart of these operations. This article demystifies AVPlayer, offering insights into its functionalities, from basic setup to advanced customizations, ensuring that you can harness its full potential in your iOS applications.
Table of Contents:
What Is AVPlayer?
AVPlayer is a simple class that enables you to manage an audio visual playback on iOS. Audio visual means you can manage both audio playback and as well as video playback.
AVPlayer is the heart of playing videos on iOS.
A player object can start and stop your videos, change their playback rate and even turn the volume up and down. Think of a player as a controller object that’s able to manage playback of one media asset at a time. The framework also provides a subclass of class, called AVQueuePlayer, you use to create and manage the queuing of media assets played sequentially.
How To Create a Basic Video Player?
In the provided code snippet, the primary function is to play a video sourced from an HTTP Streaming Live (HLS) URL using iOS’s AVPlayer and AVPlayerViewController. It initiates by creating a URL object using the provided string, which points to an ‘.m3u8’ file (a common format for HLS). If the URL is not valid, the method will return early to prevent any subsequent errors. Once the URL is established, an instance of AVPlayer is created, using this URL. The code commentary mentions an implicit creation of AVPlayerItem, which is essentially the media resource.
Explore More ✅
Stream Your Videos on iOS securely Multi-DRM EncryptionVdoCipher can help you stream your videos on iOS. You can host your videos securely, and you get various features such as Video API, CDN, Analytics, and Dashboard to manage your videos easily.
This player item can be accessed using the currentItem
property of the AVPlayer. Following this, an AVPlayerViewController is created, which provides a full-screen interface for video playback. A reference to our AVPlayer instance is then assigned to this view controller. Finally, the AVPlayerViewController is presented modally, and once it’s fully displayed, the video begins to play through the play()
method of the AVPlayer.
guard let url = URL(string: "https://example.com/my-example-video.m3u8") else { return }
// Create an AVPlayer, passing it the HTTP Live Streaming URL.
let player = AVPlayer(url: url)
// Discussion: This method implicitly creates an AVPlayerItem object. You can get the player item using currentItem.
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
player.play()
}
What are the Important APIs for AVPlayer?
These are some of the important methods and properties for a media player object:
play()
:
Initiates playback for the item that is currently set up in the player.
func play()
// Begins playback of the current item.
pause()
:
Halts the playback of the current media item, allowing for resumption from the same point later.
func pause()
// Pauses playback of the current item.
rate: Float
:
Represents the playback speed of the media. A rate of 1.0
means normal speed, while 0.0
indicates a pause. Rates greater than 1.0
will fast forward, and rates less than 0.0
would represent rewinding (if supported).
var rate: Float
// The current playback rate.
currentItem: AVPlayerItem?
:
This holds the media item that the player is currently prepared to play. It can be nil
if no item is set.
var currentItem: AVPlayerItem?
// The item for which the player is currently controlling playback.
replaceCurrentItem(with: AVPlayerItem?)
:
This method allows the swapping out of the currently playing item with a different one, facilitating dynamic playlists or a change in media.
func replaceCurrentItem(with: AVPlayerItem?)
// Replaces the current item with a new item.
currentTime() -> CMTime
:
Retrieves the current timestamp of the media playback. This allows for tracking or displaying the progress of the media playback.
func currentTime() -> CMTime
// Returns the current time of the current player item.
seek(to: CMTime)
:
Commands the player to jump to a specific timestamp in the media. This is useful for functionalities like skipping or fast forwarding to a certain point in the media.
func seek(to: CMTime)
// Requests that the player seek to a specified time.
seek(to: Date, completionHandler: (Bool) -> Void)
:
This variant of the seek method aims to jump to a specific date in the media (useful for date-based media streams) and provides a completion handler to notify when the seek operation is finished.
func seek(to: Date, completionHandler: (Bool) -> Void)
// Requests that the player seek to a specified date, and to notify you when the seek is complete.
volume: Float
:
Dictates the audio volume level of the playback. This is a floating point value where 1.0
represents full volume and 0.0
represents muted.
var volume: Float
// The audio playback volume for the player.
isMuted: Bool
:
A simple boolean property to quickly check or set if the player’s audio is currently muted.
var isMuted: Bool
// A Boolean value that indicates whether the audio output of the player is muted.
What is AVFoundation?
While AVFoundation can feel a bit intimidating, most of the objects you deal with are still pretty high-level.
The main classes you’ll need to get familiar with are:
AVPlayerLayer
This special CALayer subclass can display the playback of a given AVPlayer object.
AVAsset
These are static representations of a media asset. An asset object contains information such as duration and creation date.
AVPlayerItem
The dynamic counterpart to an AVAsset. This object represents the current state of a playable video. This is what you need to provide to AVPlayer to get things going.
AVFoundation is a huge framework that goes well beyond these few classes. Fortunately, this is all you’ll need to create your video player.
How To Visualise AVPlayer?
AVPlayer and AVPlayerItem are nonvisual objects, meaning that on their own they’re unable to present an asset’s video onscreen. There are two primary approaches you use to present your video content onscreen:
AVKit
The best way to present your video content is with the AVKit framework’s AVPlayerViewController class in iOS and tvOS, or the AVPlayerView class in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.
AVPlayerViewController
A view controller that displays content from a player and presents a native user interface to control playback.
A player view controller makes it simple to add media playback capabilities to your app that match the styling and features of the native system players. Using this object also means that your app automatically adopts the new features and styling of future operating system releases.
AVPlayerLayer
When building a custom interface for your player, use AVPlayerLayer. You can set this layer a view’s backing layer or add it directly to the layer hierarchy. Unlike AVPlayerView and AVPlayerViewController, a player layer doesn’t present any playback controls—it only presents the visual content onscreen. It’s up to you to build the playback transport controls to play, pause, and seek through the media.
How To Create Custom Interface for AVPlayer?
Creating a custom interface for AVPlayer involves building your own user interface components to control playback, display video content, and provide user interactions. Here are the steps to create a custom interface for AVPlayer:
- Create a UIView subclass that will serve as the container for your player interface. Let’s call it “PlayerView”.
import UIKit
import AVFoundation
class PlayerView: UIView {
private let playerLayer: AVPlayerLayer
override class var layerClass: AnyClass {
return AVPlayerLayer.self
}
var player: AVPlayer? {
get {
return playerLayer.player
}
set {
playerLayer.player = newValue
}
}
override init(frame: CGRect) {
playerLayer = AVPlayerLayer()
super.init(frame: frame)
playerLayer.videoGravity = .resizeAspectFill
layer.addSublayer(playerLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}
- Customize your player interface by adding controls and other UI elements as per your requirements. For example, you can add play/pause buttons, a seek slider, volume controls, and labels to display current playback time or video duration.
import UIKit
import AVFoundation
class CustomPlayerViewController: UIViewController {
// ...
private var playButton: UIButton!
private var seekSlider: UISlider!
private var currentTimeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// ...
// Add play/pause button
playButton = UIButton(type: .system)
playButton.setTitle("Play", for: .normal)
playButton.addTarget(self, action: #selector(playButtonTapped), for: .touchUpInside)
view.addSubview(playButton)
// Add seek slider
seekSlider = UISlider()
seekSlider.addTarget(self, action: #selector(seekSliderValueChanged), for: .valueChanged)
view.addSubview(seekSlider)
// Add current time label
currentTimeLabel = UILabel()
view.addSubview(currentTimeLabel)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Layout your controls
playButton.frame = CGRect(x: 20, y: view.bounds.height - 80, width: 80, height: 40)
seekSlider.frame = CGRect(x: 120, y: view.bounds.height - 80, width: view.bounds.width - 240, height: 40)
currentTimeLabel.frame = CGRect(x: view.bounds.width - 100, y: view.bounds.height - 80, width: 80, height: 40)
}
@objc private func playButtonTapped() {
if player.rate == 0 {
player.play()
playButton.setTitle("Pause", for: .normal)
} else {
player.pause()
playButton.setTitle("Play", for: .normal)
}
}
@objc private func seekSliderValueChanged() {
let time = CMTime(seconds: Double(seekSlider.value), preferredTimescale: 1)
player.seek(to: time)
}
// ...
}
3.Implement the necessary actions for your controls. In the example above, the play/pause button toggles the playback state of the player, and the seek slider allows the user to seek to a specific time in the video.
These steps provide a basic outline for creating a custom interface for AVPlayer. You can expand on this foundation by adding more controls, styling, and interactions to enhance the user experience.
Remember to handle user interactions appropriately, update UI elements based on the player’s state, and consider accessibility and usability guidelines when designing your custom player interface.
Observing Player
AVPlayer is a dynamic object whose state continuously changes. There are two approaches you can use to observe a player’s state:
General State Observations: You can use key-value observing (KVO) to observe state changes to many of the player’s dynamic properties, such as its currentItem or its playback rate.
Timed State Observations: KVO works well for general state observations, but isn’t intended for observing continuously changing state like the player’s time. AVPlayer provides two methods to observe time changes:
addPeriodicTimeObserver(forInterval:queue:using:)
addBoundaryTimeObserver(forTimes:queue:using:)
These methods let you observe time changes either periodically or by boundary, respectively. As changes occur, invoke the callback block or closure you supply to these methods to give you the opportunity to take some action such as updating the state of your player’s user interface.
AVQueuePlayer
AVQueuePlayer is a subclass of AVPlayer that allows you to create and manage a queue of media assets to be played sequentially. It provides a convenient way to handle a playlist of videos or audios without manually handling the transitions between items.
To create an AVQueuePlayer, you can initialize it with an array of AVPlayerItems:
let item1 = AVPlayerItem(url: URL(string: "https://example.com/video1.mp4")!)
let item2 = AVPlayerItem(url: URL(string: "https://example.com/video2.mp4")!)
let queuePlayer = AVQueuePlayer(items: [item1, item2])
You can then control the playback of the queue player using the same methods available in AVPlayer, such as play(), pause(), seek(to:), and replaceCurrentItem(with:).
To observe the state changes of the AVQueuePlayer, you can use key-value observing (KVO) on its currentItem property. This allows you to be notified when the current item changes and perform any necessary actions, such as updating the user interface:
queuePlayer.addObserver(self, forKeyPath: "currentItem", options: [.new, .initial], context: nil)
Make sure to implement the observeValue(forKeyPath:of:change:context:) method to handle the KVO notifications:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "currentItem", let player = object as? AVPlayer, let currentItem = player.currentItem {
// Handle the current item change
}
}
Remember to remove the observer when you no longer need it:
queuePlayer.removeObserver(self, forKeyPath: "currentItem")
AVQueuePlayer also provides additional methods for managing the queue, such as append(:), insert(:after:), and removeAllItems(). These methods allow you to dynamically modify the queue during playback.
With AVQueuePlayer, you can easily create a seamless playlist experience for your users by adding and removing items as needed. It provides a powerful tool for managing media playback in your iOS app.
How To Play DRM Content On AVPlayer using VdoCipher?
Playing DRM (Digital Rights Management) content with AVPlayer requires additional setup and integration with the appropriate DRM system. The process may vary depending on the DRM system you are using. Here are the general steps involved in playing DRM content with AVPlayer:
- Choose a DRM system: Select the DRM system that is compatible with your content and platform. Popular DRM systems include Apple FairPlay, Google Widevine, and Microsoft PlayReady.
- Obtain DRM credentials: Contact the DRM service provider or content provider to obtain the necessary credentials, such as content keys or authorization tokens, to access and decrypt the DRM-protected content.
- Integrate DRM framework: Depending on the DRM system, you need to integrate the corresponding DRM framework into your app. For example, Apple FairPlay requires the use of FairPlay Streaming (FPS) framework, Widevine requires the Widevine DRM framework, and PlayReady requires the PlayReady DRM framework.
- Configure AVAssetResourceLoaderDelegate: Implement the AVAssetResourceLoaderDelegate protocol to handle resource loading and decryption for DRM-protected content. This delegate allows you to intercept the loading of media resources and provide necessary DRM-related information.
How To Enable Adaptive Bitrate Streaming With AVPlayer?
Adaptive Bitrate Streaming (ABR) is a technique used in video streaming to dynamically adjust the quality of the video based on the viewer’s network conditions. AVPlayer supports ABR through its integration with HTTP Live Streaming (HLS), which is a widely used streaming protocol that supports ABR.
To enable adaptive bitrate streaming in AVPlayer, you need to provide an HLS manifest file (usually in the form of an M3U8 playlist) that contains multiple versions of the video encoded at different bitrates. AVPlayer will automatically switch between different bitrate versions based on network conditions to provide the best possible viewing experience.
Here are the steps to enable adaptive bitrate streaming with AVPlayer:
Prepare your video assets: Encode your video at different bitrates and create multiple versions of the video files. Typically, you would encode the video into different quality levels, such as SD, HD, and 4K, each with different bitrates and resolutions.
Generate an HLS manifest: Create an M3U8 playlist file that serves as the HLS manifest. This playlist file should include the URLs to the different bitrate versions of the video. Each entry in the playlist corresponds to a specific quality level (bitrate) of the video. The manifest should also contain information about the duration, segments, and other metadata related to the video.
Host the HLS manifest and video segments: Host the HLS manifest file and the corresponding video segments on a web server or a content delivery network (CDN) that supports HTTP Live Streaming. Ensure that the server provides the necessary CORS headers to allow AVPlayer to access the resources. An efficient M3U8 player ensures seamless playback of your streaming playlists, providing viewers with a high-quality experience
Create an AVURLAsset: Create an instance of AVURLAsset using the URL of the HLS manifest file.
Swift AVPlayer vs. Objective-C AVPlayer
Swift, in the context of your question about AVPlayer, refers to the programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development. Swift was introduced in 2014 as a modern replacement for Objective-C, offering a more powerful and intuitive way to write code for Apple platforms. AVPlayer itself is the same underlying class regardless of whether you’re using it in a Swift or Objective-C project. However, the differences arise from the language features and paradigms.
Using AVPlayer in Swift
- Syntax and Language Features: Swift offers modern syntax and features like type inference, optionals, and powerful error handling that make working with AVPlayer more streamlined and safer compared to Objective-C.
- Closures: Swift’s closures (similar to blocks in Objective-C but more powerful) are used extensively for handling asynchronous events and completion handlers.
- Optionals: Swift’s strong type system and use of optionals help in handling nil values explicitly, reducing runtime crashes.
- Extensions: Swift makes it easy to extend AVPlayer’s functionality with extensions, adding custom methods or computed properties without subclassing.
- Generics and Protocols: Swift’s generics and protocol-oriented programming can be leveraged to create more reusable and flexible code when working with media playback.
Aspect | Swift | Objective-C |
Syntax | More concise and expressive. | More verbose and complex. |
Error Handling | Uses do-try-catch blocks for structured error handling. |
Uses NSError pointers for error handling, which is less structured.
|
Memory Management | Automatic Reference Counting (ARC) with additional safeguards like weak and unowned references to prevent retain cycles. |
Also uses ARC, but managing retain cycles often requires more manual intervention with weak and unsafe_unretained.
|
Modern Language Features | Features like pattern matching, enums with associated values, and protocol extensions for elegant solutions to complex problems. |
Lacks many modern language features; relies on traditional object-oriented programming paradigms.
|
Conclusion | Results in cleaner, more maintainable, and safer code due to modern features and a strong type system, leading to a more efficient development process and fewer bugs. |
Often results in more boilerplate code and can be prone to more runtime errors, making the development process potentially more cumbersome.
|
FAQs
Can AVPlayer play videos in the background?
Yes, to enable background video playback, you need to configure your app’s audio session and plist settings. Specifically, set your AVAudioSession category to AVAudioSessionCategoryPlayback and enable the “Audio, AirPlay, and Picture in Picture” background mode in your app’s Info.plist.
Can AVPlayer stream live content?
Yes, AVPlayer can stream live HTTP content. You initialize an AVPlayerItem with the URL of the live stream and then play it with AVPlayer just as you would with recorded content.
How does AVPlayer differ from AVAudioPlayer?
AVPlayer is designed for both audio and video playback and offers more features, such as the ability to play content from a network stream. AVAudioPlayer is simpler and intended only for audio playback from files or NSData objects.
Conclusion
AVPlayer is a fundamental class in iOS for managing audio and video playback. It allows you to create basic video players, control playback, observe state changes, and even create advanced features like playlists using AVQueuePlayer. By leveraging AVFoundation, you can build custom video players or use the AVKit framework to provide a native playback experience with minimal effort. Understanding AVPlayer and its related classes will empower you to create rich multimedia experiences in your iOS applications.
Navigating the vast ecosystem of iOS development tools can often be daunting, but when it comes to multimedia playback, AVPlayer undeniably stands out as a game-changer. Its versatility and depth, ranging from basic media playback to intricate configurations, make it a quintessential tool for any developer aiming to deliver rich multimedia experiences in their apps. By mastering AVPlayer and the underlying AVFoundation framework, we not only elevate the user experience but also open doors to new creative possibilities. Whether you’re just starting or refining your skills, the journey with AVPlayer promises both challenges and rewarding outcomes, charting a path toward the zenith of iOS multimedia integration.
Feel free to reach out if you have any further questions. Happy coding!
Supercharge Your Business with Videos
At VdoCipher we maintain the strongest content protection for videos. We also deliver the best viewer experience with brand friendly customisations. We'd love to hear from you, and help boost your video streaming business.
A firm believer in numbers and logic. A family guy loves traveling, doing adventure sports, follows a variety of sports, addicted to forehand chop table tennis shot!