Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MPMoviePlayerViewController在iPhone模拟器上播放视频_Iphone_Objective C_Ios_Xcode_Cocoa Touch - Fatal编程技术网

使用MPMoviePlayerViewController在iPhone模拟器上播放视频

使用MPMoviePlayerViewController在iPhone模拟器上播放视频,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我正在使用MPMoviePlayerViewController播放视频。我已经添加了下面的代码,但是它没有播放。视图显示为黑屏,并且在不播放的情况下立即隐藏 MPMoviePlayerViewController *moviePlayerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:@"/Users/gui_test/Desktop/273_0.mp4"]]; mov

我正在使用MPMoviePlayerViewController播放视频。我已经添加了下面的代码,但是它没有播放。视图显示为黑屏,并且在不播放的情况下立即隐藏

 MPMoviePlayerViewController  *moviePlayerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:@"/Users/gui_test/Desktop/273_0.mp4"]];
    moviePlayerVC.moviePlayer.allowsAirPlay = YES; 
    moviePlayerVC.view.backgroundColor = [UIColor blackColor];
    [self presentMoviePlayerViewControllerAnimated:moviePlayerVC];
    [moviePlayerVC.moviePlayer play];

我是否需要做更多的事情来播放mp4文件注:我正在模拟器中尝试。是否可以在模拟器中播放(.mp4)文件。

我使用了MPMoviePlayerController而不是MPMoviePlayerViewer控制器:

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:songUrl];
[[self.moviePlayer view] setBounds:CGRectMake(0, 0, 320, 480)];
[moviePlayer.backgroundView setBackgroundColor:[UIColor blackColor]];   
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setScalingMode:MPMovieScalingModeNone];
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:NO];
试试这个应该可以。

试试这个代码:),我猜它只支持.mov文件,而你的视频没有播放,因为它是mp4

NSString*thePath=[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"mov"];
NSURL*theurl=[NSURL fileURLWithPath:thePath];
NSLog(@"the url= %@",theurl);

moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 480)];
[moviePlayer prepareToPlay];
[moviePlayer setFullscreen:YES];
[moviePlayer setShouldAutoplay:YES]; 
[self.view addSubview:moviePlayer.view];

您无法访问沙箱之外的文件。使用应用程序包或应用程序的文档目录中的文件。@Carlvaeze我已将文件复制到documentsDirectory中,并尝试替换路径。它只显示黑屏。有什么想法吗?你能发布更新的代码吗?@carlvaezey NSURL*loc_url=[NSURL URLWithString:@”/Users/gui_test/Library/Application Support/iPhone Simulator/5.1/Applications/96C4CA5F-E308-4F3D-BF63-EEE061E7A80D/Documents/cmpny/273/273_0.mp4“;我在这里得到空url。这可能就是问题所在。我已经在这里复制了文档路径,可以吗?如果你把它硬编码到你的应用程序中,那就不是这样做的。为什么不先将电影添加到xcode项目中,然后使用NSBundle
urlForResource:withExtension:subDirectory
方法获取电影的URL呢。然后看看它是否播放。