Understanding AVPlayerViewController in iOS 8: A Step-by-Step Guide
Introduction
The AVPlayerViewController is a powerful tool for playing video content in an iOS application. It provides a convenient and user-friendly way to display videos, with features like playback controls and full-screen mode. However, in order to use the AVPlayerViewController, you need to ensure that all necessary components are properly set up.
Troubleshooting AVPlayer not loading video in iOS 8
The Problem: No Video Loading
In the provided Stack Overflow question, it is revealed that the video does not load when using the AVPlayerViewController in iOS 8. This issue can be attributed to a common mistake made by developers: forgetting to include the video file in the target.
What are Targets?
In Xcode, a target refers to a project configuration that defines the build settings for a specific build product. When you create an app, you typically have multiple targets, each corresponding to a different architecture (e.g., iPhone 6S, iPad Pro) or a different deployment configuration (e.g., debug, release).
The Importance of Target Membership
When using the AVPlayerViewController, it is crucial that the video file is included in the target membership. This ensures that the file exists at runtime and can be loaded by the player.
Setting up Video File for Target Membership
- Open your Xcode project.
- In the Project Navigator, navigate to the
Assets.xcassetsfolder (or equivalent). - Select the video file you want to play (e.g.,
Besan.mp4) in the navigator. - In the Utilities sidebar, choose the File Inspector.
- Ensure that your app target is selected under “Target Membership” in the File Inspector.
Example of Correct Target Membership

By selecting the correct target membership for the video file, you ensure that it exists at runtime and can be loaded by the AVPlayerViewController.
Verifying Video File Existence
To verify that the video file exists and is correctly included in the target membership, follow these steps:
- Open your Xcode project.
- In the Project Navigator, navigate to the
Assets.xcassetsfolder (or equivalent). - Select the video file you want to play (e.g.,
Besan.mp4) in the navigator. - Use the Quick Look feature to inspect the file:
- Press
Cmd + Fto open Quick Look. - Click on the video file to view its contents.
- Press
Playing Video with AVPlayerViewController
Once you have set up the video file for target membership, you can proceed to play the video using the AVPlayerViewController.
Creating and Initializing AVPlayerViewController
{< highlight language="objc" >}
// Create a new instance of AVPlayerViewController
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
// Set the URL for the player
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Besan" withExtension:@"mp4"];
playerViewController.player = [AVPlayer playerWithURL:url];
// Present the controller modally
[self presentViewController:playerViewController animated:YES completion:nil];
{/highlight}
Code Explanation
In this example, we create a new instance of AVPlayerViewController and set its player property to an instance of AVPlayer. The AVPlayer is initialized with a URL pointing to the video file. Finally, we present the controller modally.
Best Practices for Using AVPlayerViewController
To ensure that your app runs smoothly and efficiently, follow these best practices when using the AVPlayerViewController:
- Use full-screen mode sparingly, as it can cause performance issues.
- Implement playback controls to provide users with better control over playback.
- Handle errors and exceptions properly to ensure a smooth user experience.
Troubleshooting AVPlayer not loading video in iOS 8: Common Issues
No Video File Found
When the video file is not found, it can be due to various reasons:
- The video file is missing from the
Assets.xcassetsfolder. - The target membership for the video file is incorrect.
Incorrect Target Membership
If the target membership for the video file is incorrect, you may need to review your project settings and ensure that the correct target membership has been selected.
Conclusion
In this article, we discussed how to troubleshoot common issues with the AVPlayerViewController in iOS 8. By following the steps outlined above, you can ensure that your app runs smoothly and efficiently, providing users with a better experience.
Last modified on 2023-05-30