【问题标题】:How to play a video from either a local or a server URL in iOS如何在 iOS 中从本地或服务器 URL 播放视频
【发布时间】:2011-05-17 06:12:57
【问题描述】:

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

【问题讨论】:

标签: ios


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请试一试,它对我很有效,

    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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 2011-09-02
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多