如何在 iOS 中从本地或服务器 URL 播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6026942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:57:15  来源:igfitidea点击:

How to play a video from either a local or a server URL in iOS

ios

提问by Nikhil

How can I play a .mp4 or .mov video from either an Internet URL or a local file in iOS?

如何从 Internet URL 或 iOS 中的本地文件播放 .mp4 或 .mov 视频?

回答by Mehul Mistri

1.First of all add MediaPlayer.Framework in XCode

1.首先在XCode中添加MediaPlayer.Framework

2.Then add #import < MediaPlayer/MediaPlayer.h > in your viewController's .h file

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

3.Now implement this code in your viewDidLoad Method

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];  

For Orientation Please add this code

方向请添加此代码

 - (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;
 }

In this code moviePlayerController is MPMoviePlayerController declared in .h file

在这段代码中,moviePlayerController 是在 .h 文件中声明的 MPMoviePlayerController

回答by Bourne

This is an old question but still relevant and iOS 9 has deprecated MPMoviePlayerController. The new thing to use if AVMoviePlayer, example code:

这是一个老问题,但仍然相关,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];

回答by arunprasath sivasamy

Please try this its perfectly working for me,

请试试这个它对我来说完美无缺,

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

}

And please enable the App Transport Security Settings -->Allow Arbitrary Loads-->YESin Plist file.

并请在 Plist 文件中启用应用程序传输安全设置 --> 允许任意加载--> 是