Creating a simple video player

The first thing you need to do to implement a video player is to obtain a video file. You can use any video that is encoded in the h.264 format. A good sample video is the Big Buck Bunny sample movie that was created by the Blender Foundation. You can find this video at the following URL: http://bbb3d.renderfarming.net/download.html. If you want to use this video to practice with, make sure to download the 2D version of the video.

As stated before, you will implement the video player using the AVPlayerViewController. This view controller provides a convenient wrapper around several components from AVFoundation, and also provides default video controls, so you don't have to build your entire video player from scratch, as you will do for the audio player later.

The AVPlayerViewController is highly configurable, which means that you can choose whether the player supports Airplay, shows playback controls, whether it should be full screen when a video plays, and more. For a complete list of configurable options, you can refer to Apple's AVPlayerViewController documentation.

Once you have found your test video, you should add it to the MediaPlayback project and ensure that the video is added to the app target. After doing this, open VideoViewController.swift and add the following line to import AVKit:

import AVKit

You should also add a property to VideoViewController to hold on to your video player instance. Add the following line to the VideoViewController class to do this:

let playerController = AVPlayerViewController()

Since the AVPlayerViewController is a UIViewController subclass, you should add it to the VideoViewController as a child view controller. Doing this will make sure that VideoViewController forwards any view controller lifecycle events, such as viewDidLoad(), along with any changes in trait collections and more to the video player. To do this, add the following code to the viewDidLoad() method in VideoViewController:

// 1
addChild(playerController)
playerController.didMove(toParent: self)

// 2
view.addSubview(playerController.view)

let playerView = playerController.view!
playerView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
  playerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -20),
  playerView.heightAnchor.constraint(equalTo: playerView.widthAnchor, multiplier: 9/16),
  playerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  playerView.centerYAnchor.constraint(equalTo: view.centerYAnchor)])    

The previous code snippet adds the video player to the video view controller as a child view controller. When you add a view controller as a child view controller, you must always call didMove(toParent:) on the child controller to make sure it knows that it has been added as a child view controller to another view controller. After adding the video player as a child view controller, the video player's view is added as a subview for the video view controller, and some constraints are set up to position the player view.

This is all you need to do to create an instance of the video player and make it appear in your view controller. The last step is to obtain a reference to your video file, create an AVPlayer that has a reference to the video file, and assign it to the player. Add the following code to do this:

let url = Bundle.main.url(forResource: "samplevideo", withExtension: "mp4")!
playerController.player = AVPlayer(url: url)

The preceding code looks for a video file called samplevideo.mp4 and obtains a URL for that file. It then creates an instance of AVPlayer that points to that video file, and assigns it to the video player. The AVPlayer object is responsible for actually playing the video file. The AVPlayerViewController instance uses the AVPlayer instance to play the video, and manages the actual playback of the video internally.

If you run your app after adding the player this way, you will find that the video plays perfectly well, and that you have access to all the controls you might need. This is a great demonstration of how simple it is to add basic media integration to your app. The next step is a little more complex. You will directly use an AVAudioPlayer instance to play an audio file that is controlled through several custom media controls. The player will even play audio in the background and integrate with the lock screen to show information about the current file. In other words, you will build a simple audio player that does everything a user would expect it to do.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.79.70