如何从iOS中的本地或服务器URL播放video

如何从iOSurl或iOS中的本地文件播放.mp4或.movvideo?

首先在XCode中添加MediaPlayer.Framework

2.然后在viewController的.h文件中添加#import <MediaPlayer / MediaPlayer.h>

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

这是一个老问题,但仍然相关,iOS 9已经弃用MPMoviePlayerController。 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]; 

请尝试这个完美的为我工作,

 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文件中的应用传输安全设置 – >允许任意负载 – >是