Create a simple video playback app with built-in controls.
Using AVKit and AVFoundation, you can quickly create a simple video playback app.
Set Up the Audio Session
You use an audio session to communicate to the system how you intend to use audio in your app. The audio session acts as an intermediary between your app and the operating system—and, in turn, the underlying audio hardware. Configure the audio session to give your app the audio behavior expected of a media playback app.
import AVFoundationto add the AVFoundation framework to the
application:method, retrieve the shared instance of the audio session and set the app’s audio session category to
did Finish Launching With Options:
AVAudioand mode to
Session Category Playback
Session Mode Movie Playback
Set Up and Configure the User Interface
After configuring your app’s audio session, you need to create the user interface for the player.
Mainfile. In the Library’s search field, type
buttonto find the
Buttonobject into the View Controller Scene’s view and give it the title Play Video.
Add alignment constraints to center the button both horizontally and vertically.
Implement Playback Behavior
Now that you have created the user interface, it’s time to add the code required to play a video.
In the Project Navigator, select the
Mainfile and open the assistant editor.
Control-drag from the Play Video button to the
Viewclass to add a new
Close the assistant editor and select the
Viewclass in the Project Navigator. Above the class definition, import the AVKit and AVFoundation frameworks.
playmethod, add the following implementation:
Your app is complete, and you can run it in the Simulator or on your iOS or tvOS device.