【发布时间】:2011-05-17 06:12:57
【问题描述】:
如何从 Internet URL 或 iOS 中的本地文件播放 .mp4 或 .mov 视频?
【问题讨论】:
标签: ios
如何从 Internet URL 或 iOS 中的本地文件播放 .mp4 或 .mov 视频?
【问题讨论】:
标签: ios
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
【讨论】:
请试一试,它对我很有效,
var moviePlayer:MPMoviePlayerController!
override func viewDidLoad()
{
super.viewDidLoad()
let url:NSURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 20, width: UIScreen.mainScreen().bounds.size.width-40, height: UIScreen.mainScreen().bounds.size.height/2)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded
}
请在 Plist 文件中启用 App Transport Security Settings -->Allow Arbitrary Loads-->YES。
【讨论】:
这是一个老问题,但仍然相关,iOS 9 已弃用 MPMoviePlayerController。 if AVMoviePlayer 使用的新东西,示例代码:
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"vid" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = self.view.bounds;
[self.view.layer addSublayer: layer];
[self.avPlayer play];
【讨论】: