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

时间:2011-05-17 06:12:57

标签: ios

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

3 个答案:

答案 0 :(得分:28)

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

2.然后添加#import< MediaPlayer / MediaPlayer.h>在viewController的.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];  

For Orientation请添加此代码

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

答案 1 :(得分:9)

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

答案 2 :(得分:3)

请尝试将其完美地用于我,

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