更新时间:2023-09-22 09:45:28
这很奇怪,但是如果将MPMoviePlayerController设置为属性而不是局部变量,则似乎可以正常工作.似乎幕后正在发生某些事情.我认为这与ARC有关.您在使用ARC吗?
It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?
这也是您过度添加路径的问题:
It's also an issue that you've over-appended your path:
// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
当我在模拟器中运行您的代码时,路径如下所示:
When I run your code in the simulator, the path looks like this:
/用户/mlong/库/应用程序支持/iPhone 模拟器/5.1/应用程序/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
应该就是这样:
/用户/mlong/库/应用程序支持/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
/Users/mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
所以只需删除这一行:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
然后将您的播放器实例更改为:
And then change your player instantiation to this:
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];
因此,您应该将MPMoviePlayerController添加为包含的视图控制器的属性.
So you should add the MPMoviePlayerController as a property of your containing view controller.