Video playback support in iOS is provided by combining the AVFoundation AVPlayer and AVKit AVPlayerViewController classes.
This chapter presents an overview of video playback in iOS using these two classes, followed by a step-by-step example.
The AVPlayer and AVPlayerViewController Classes
The sole purpose of the AVPlayer class is to play media content. An AVPlayer instance is initialized with the
URL of the media to be played (either a path to a local file on the device or the URL of network-based media). Playback can be directed to a device screen or, in the case of external playback mode, via AirPlay or an HDMI/ VGA cable connection to an external screen.
The AVKit Player View Controller (AVPlayerViewController) class provides a view controller environment through which AVPlayer video is displayed to the user, together with several controls that enable the user to manage the playback experience. Playback may also be controlled from within the app code by calling the play and pause methods of the AVPlayer instance.
The iOS Movie Player Example App
The remainder of this chapter aims to create a simple app to play a video when a button is pressed. The video will be streamed over the internet from a movie file on a web server.
Launch Xcode and create a new project using the iOS App template with the Swift and Storyboard options selected, entering AVPlayerDemo as the product name.
Designing the User Interface
Select the Main.storyboard file and display the Library. Next, drag a single Button instance to the view window and change the text on the button to “Play Movie”. With the button selected in the storyboard canvas, display the Auto Layout Align menu and add horizontal and vertical container constraints to the view with offset values set to 0.
From the Library panel, locate the AVKit Player View Controller object and drag it onto the storyboard to the right of the existing view controller. Ctrl-click on the button in the first view controller and drag the resulting line to the AVKit Player View Controller. Release the line and select Show from the segue selection menu.
Initializing Video Playback
When the user taps the “Play Movie” button, the app will perform a segue to the AVPlayerViewController scene, but as yet, no AVPlayer has been configured to play video content. This can be achieved by implementing the prepare(for segue:) method within the ViewController.swift file as follows, the code for which relies on the AVKit and AVFoundation frameworks having been imported:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
.
.
.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as!
AVPlayerViewController
let url = URL(string:
"https://www.ebookfrenzy.com/ios_book/movie/movie.mov")
if let movieURL = url {
destination.player = AVPlayer(url: movieURL)
}
}
.
.
Code language: Swift (swift)
The code in this above method begins by obtaining a reference to the destination view controller, in this case, the AVPlayerViewController instance. A URL object is then initialized with the URL of a web-based video file. Finally, a new AVPlayer instance is created, initialized with the video URL, and assigned to the player property of the AVPlayerViewController object.
Running the app once again should cause the video to be available for playback within the player view controller scene.
Build and Run the App
With the design and coding phases complete, all that remains is to build and run the app. First, click on the run button in the toolbar of the main Xcode project window. Assuming no errors occur, the app should launch within the iOS Simulator or device. Once loaded, touching the Play Movie button should launch the movie player in full-screen mode, and playback should automatically begin:
Creating an AVPlayerViewController Instance from Code
The example shown in this chapter used storyboard scenes and a transition to display an AVPlayerViewController instance. While this is a quick approach to working with the AVPlayerViewController and AVPlayer classes, the same result may be achieved directly by writing code within the app. The following code fragment, for example, initializes and plays a video within an app using these two classes without the use of a storyboard scene:
let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
Code language: Swift (swift)
Summary
The basic classes needed to play back video from within an iOS app are provided by the AVFoundation and AVKit frameworks. The purpose of the AVPlayer class is to facilitate the playback of video media files. The AVPlayerViewController class provides a quick and easy way to embed an AVPlayer instance into a view controller environment with a set of standard on-screen playback controls.