【问题标题】:iOS play video with MPMoviePlayerControlleriOS 使用 MPMoviePlayerController 播放视频
【发布时间】:2013-07-08 20:58:49
【问题描述】:

我得到了这段代码:

theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:@"/Resources/disc.mp4"]];
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
    [theMoviPlayer.view setFrame:backgroundWindow.frame];
    [backgroundWindow addSubview:theMoviPlayer.view];
    [theMoviPlayer play];

但我真的不知道如何将视频添加到我的项目中。我必须将视频文件放在哪个文件夹中!?还是我必须做其他事情才能将其添加到我的项目中?

编辑:

在 xcode 中看起来像这样,对吗? 因为我现在确实收到播放错误。 以前我用一个 url 播放这个视频,效果很好,但是在本地没有这个文件:(

【问题讨论】:

    标签: iphone ios ios5 ios6 ios6.1


    【解决方案1】:

    好的,你的包路径看起来很复杂,下面应该可以工作。

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
    NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    
    theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
    theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
    UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
    [theMoviPlayer.view setFrame:backgroundWindow.frame];
    [backgroundWindow addSubview:theMoviPlayer.view];
    [theMoviPlayer play];
    

    【讨论】:

    • 有效!十分感谢。我对iOS开发很陌生。我的方法没有按预期工作的原因是什么?
    • 如果你想使用 UIWebView,你可以使用 HTML5 来播放视频,如果你更舒服的话。我将在下面附上代码。
    • 如果您想玩它,请附上它作为答案。
    【解决方案2】:

    添加媒体播放器框架

    将其导入您的文件

    #import <MediaPlayer/MediaPlayer.h>
    

    创建 MPMoviePlayerController 的对象

    MPMoviePlayerController * moviePlayer;
    

    在你想播放视频的地方写下这段代码

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"spacetest.mp4" ofType:nil];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [self.view addSubview:moviePlayer.view];
    moviePlayer.fullscreen = YES;
    [moviePlayer play];
    

    【讨论】:

      【解决方案3】:

      按照我上面的承诺使用 HTML5:

          NSString *videoTitle = @"disc.mp4";
          NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
          NSString *playPath = [NSString stringWithFormat:@"<center><video  width=\"640\" height=\"480\" controls><source src=\"%@\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle];
      
      
          [webView loadHTMLString:playPath baseURL:baseURL];
      

      这将以 640x480 播放,但如果您熟悉 HTML5 视频标签,则可以进行大量自定义。

      【讨论】:

      • 毫无疑问。我大部分时间都使用 HTML5 进行播放。它的某些东西使它感觉更干净。加上 WebKit,它允许比 iOS 的多媒体框架更多的自定义。
      【解决方案4】:

      由于您使用的是 MPMoviePlayerController 而不是 UIWebView,因此您可以将 mp4 或文件放在资源中,XCode/iOS 会找到它。确保文件所在的目录/组是黄色而不是蓝色。您不希望它是相对路径。

      只需将资源拖到您的项目中即可。选择将项目复制到目标,选择文件夹的第一个选项,最重要的是,添加到目标!

      好的,试试下面的代码:

      NSBundle *bundle = [NSBundle mainBundle];
      NSString *moviePath = [bundle pathForResource:@"disc" ofType:@"mp4"];
      NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
      
      theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
      theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
      theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
      UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
      [theMoviPlayer.view setFrame:backgroundWindow.frame];
      [backgroundWindow addSubview:theMoviPlayer.view];
      [theMoviPlayer play];
      

      【讨论】:

      • 也应该使用NSBundle获取路径
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 2013-10-10
      • 2012-05-11
      相关资源
      最近更新 更多