我创建了一个显示文件夹中文件的应用程序。 我有一个 .mov 格式的文件,我想从文档文件夹中显示它,但是当运行应用程序并单击播放按钮时,不显示这部电影,我看到了这张图片 :
这是我的代码:(请指导我并告诉我我的错误)
- (IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:_moviePlayer.view];
_moviePlayer.fullscreen = YES;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.allowsAirPlay = YES;
[_moviePlayer play];
}
编译器也给我按摩:
movie player[9489:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
movie player[9489:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
movie player[9489:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
最佳答案
在添加电影播放器之前需要检查文件是否存在,也可以添加默认
-(IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
if(fileExists)
{
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayer];
_moviePlayer.shouldAutoplay=NO;
[_moviePlayer prepareToPlay];
}
}
在电影完全加载后添加你的电影
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
_moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer play];
[_moviePlayer setFullscreen:YES animated:YES];
}
关于ios - 不要在 xcode 的文档文件夹中显示电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557241/