Display a preview of an upcoming media item at the conclusion of the currently playing media item.
Media apps presenting serialized content, such as a TV show, often display a preview of the next episode in the series when you finish watching the current one. The user interface for this preview usually contains artwork and information about the proposed content. It also includes options for the user to either watch the next episode or return to the main menu. You add this functionality to your app using AVKit’s content proposals.
Create a Content Proposal
You create a content proposal using the AVContentProposal class. This type models the data about the proposed content, such as its title, preview image, metadata, and content URL, and the time at which to present the proposal. You create and configure a content proposal instance as shown in the following code:
Create the Content Proposal's User Interface
In addition to defining your content proposal’s data, you also need to create an interface to present this data to the user. You create this interface by subclassing the AVKit framework’s AVContentProposalViewController class. At runtime, the system passes your subclass a reference to the current AVContentProposal, providing you the data to present. Your user interface should provide visual and descriptive information about the proposed content, and should also include options for the user to accept or reject the proposal.
When the system presents your proposal, it displays it over the currently playing full-screen video. You may want to scale this video to a smaller size so you can make more room to display the details of the proposed content. To do this, you override the view controller’s preferredPlayerViewFrame property and return the desired video frame.
When the system presents the proposal, the player’s view automatically animates to the specified frame.
Add Controls to the Content Proposal
Your presented user interface should also provide controls so the user can accept or reject the proposal. The event handlers for these actions should call the controller’s dismissContentProposalForAction:animated:completion: method, indicating the user’s choice.
Make the Content Proposal Eligible to be Presented
To make your content proposal eligible for the system to present, set it as the nextContentProposal property value of the current AVPlayerItem. The following example shows how to configure this property value in a playback app that manages a queue of Video objects, which is a custom value type that models the data of an individual video in the queue. The example code creates the required playback objects, creates a new AVContentProposal for the next video in the queue, and sets the video as the player item’s nextContentProposal.
Present the Content Proposal
With the content proposal set as the player item’s nextContentProposal, the next step is to implement the methods of the AVPlayerViewControllerDelegate protocol. You use these methods to define how the system presents the content proposal, as well as to handle the acceptance or rejection of the proposed content.
If the presented AVContentProposal provides a valid content URL, the player view controller can automatically handle its acceptance or rejection. However, if you need more control over the handling of these actions, implement the playerViewController:didAcceptContentProposal: and playerViewController:didRejectContentProposal: methods. For example, the following code implements the playerViewController:didAcceptContentProposal:method to play the proposed video and create a new content proposal for the next video in the queue.