如何从Internet URL或iOS中的本地文件播放.mp4或.mov视频?
最佳答案
1.首先在XCode中添加MediaPlayer.Framework
2.然后在viewController的.h文件中添加#import
3.现在在您的viewDidLoad方法中实现此代码
//NSString *filepath = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"mp4"];
//NSURL *fileURL = [NSURL fileURLWithPath:filepath];
NSURL *fileURL = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
对于定向,请添加此代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)];
}
return YES;
}
在此代码中,moviePlayerController是.h文件中声明的MPMoviePlayerController