如何在可折叠UITableViewCell中播放本地视频?
Fixing Local Video Playback in UITableView didSelectRowAtIndexPath (Objective-C)
Hey there! Let's get that local video playing for you. First, let's break down the issues in your current code and walk through the fixes step by step.
First, Check Your Video File Setup
Before diving into code, double-check these basics—they're easy to miss:
- Make sure your
PrelimFig1.m4vfile is actually added to your Xcode project (not just dragged into the project folder outside Xcode). - When adding the file, tick the checkbox for your app target in the "Add to targets" section so it gets included in the app bundle.
- Verify the file extension is correct (no typos like
.M4Vinstead of.m4v—iOS is case-sensitive for bundle files).
Fixing Your Video Playback Code
Your core approach with AVPlayerViewController is on the right track, but there are a few small issues causing the errors:
- Typos: You wrote
presentviewcontrollerinstead of the correct camelCasepresentViewController. - Playback Timing: Calling
[player play]immediately before the view controller is presented can cause issues, since presentation is asynchronous. - Missing Safety Checks: You didn't verify if the video file exists in the bundle, which would silently fail if the path is wrong.
Here's the corrected, full implementation for your didSelectRowAtIndexPath method:
// Don't forget to import these headers at the top of your view controller file #import <AVKit/AVKit.h> #import <AVFoundation/AVFoundation.h> - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Adjust this condition to target the row where you want video playback if (indexPath.section == 1 && indexPath.row == 0) { // Get the path to your local video file NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"PrelimFig1" ofType:@"m4v"]; // Check if the file exists in the bundle if (!videoPath) { NSLog(@"Error: Video file not found in app bundle!"); return; } NSURL *videoURL = [NSURL fileURLWithPath:videoPath]; NSLog(@"Video URL: %@", videoURL); // Initialize player and player view controller AVPlayer *player = [AVPlayer playerWithURL:videoURL]; AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init]; playerVC.player = player; // Present the player view controller (fixed camelCase spelling) [self presentViewController:playerVC animated:YES completion:^{ // Start playback AFTER the view controller is fully presented [player play]; }]; } else { // Your existing row selection print logic if (indexPath.section == 0) { indexPath.row == 0 ? printf("hello\n") : printf("good bye\n"); } else { printf("good bye again\n"); } } }
Key Improvements Explained
- File Existence Check: The
videoPathcheck catches cases where the video isn't properly bundled, so you'll get a clear log message instead of silent failure. - Correct Presentation Method: Fixed the
presentViewControllerspelling mistake that was causing errors. - Asynchronous Playback: Starting playback inside the
completionblock ensures the player view controller is fully loaded before trying to play the video. - Cleaner Logic: Separated video playback code from your existing print statements for better readability.
If You Still Run Into Issues
- Check the Xcode console for specific error messages—they'll tell you exactly what's wrong (e.g., a missing file, a presentation hierarchy issue).
- Ensure your view controller is a valid presenter (it shouldn't be a child view controller without proper presentation setup, but this isn't an issue for most standard UITableView setups).
内容的提问来源于stack exchange,提问作者MFAZ
相关产品推荐
相关产品推荐

