【问题标题】:How to play video using MPMoviePlayerController?如何使用 MPMoviePlayerController 播放视频?
【发布时间】:2012-05-30 10:17:03
【问题描述】:

我正在使用以下代码使用 MPMoviePlayerController 播放视频,但视频没有播放。谁能告诉我为什么?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];

    [[moviePlayer view] setFrame:[[self view] bounds]];
    [[self view] addSubview: [moviePlayer view]];


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

    [moviePlayer play];

【问题讨论】:

  • 您是否指定了文件的扩展名?

标签: iphone objective-c ios mpmovieplayercontroller mpmovieplayer


【解决方案1】:

这很奇怪,但是如果您将 MPMoviePlayerController 设为属性而不是局部变量,它似乎可以正常工作。似乎在幕后发生了什么事。我认为这与ARC有关。你在使用 ARC 吗?

过度附加路径也是一个问题:

// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

当我在模拟器中运行您的代码时,路径如下所示:

/Users/mlong/Library/Application Support/iPhone 模拟器/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone 模拟器/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

应该是这样的:

/Users/mlong/Library/Application Support/iPhone 模拟器/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4

所以只需删除这一行:

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];

然后将您的播放器实例更改为:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

[_moviePlayer play];

因此,您应该将 MPMoviePlayerController 添加为包含视图控制器的属性。

【讨论】:

  • 看来你有一个明确的描述。我早就解决了这个问题。问题在于设置框架并将preparetoPlay和fullscreen添加为YES。
  • 嗨@Ancientar。您可以在上面的评论中看到我提到过,我忘记为 MPMoviePlayerController 设置框架,还添加了一个属性 prepareToPlay。成功了
  • 就我而言,它的 ARC 问题。我已经制作了媒体播放器的属性,它对我有用,感谢您的回答
【解决方案2】:

好吧,app bundle 和文档目录之间有很大的不同。我建议你看看那个。

首先,视频存储在哪里?

如果您的视频位于文档目录中,请不要将文档目录路径附加到捆绑路径。

试试filePath 变量:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];

但是,如果该文件位于应用程序包中(您已将其添加到 XCode 中的项目中),则应使用 jinx 响应中的内容。

【讨论】:

  • 我也试过 FilePath ,屏幕变黑但视频没有播放。
  • 仅供参考,我不会将文件添加到我的项目中,我只想从 Documents 目录中获取它
  • 任何非常简单的例子,说明如何使用 MPMoviePlayerController 播放视频就足够了。如果您能帮我解决这个问题,我将非常感谢您
  • 是的,问题可能是视频甚至不存在。你是从某个地方下载的吗?它如何到达文档目录?
  • 我已经复制粘贴在那里了。它适用于 Webview 案例。 [网络视图加载请求:urlRequest];在 webview 中加载文件,我可以看到它的播放
【解决方案3】:
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
        [moviePlayer.view setFrame:CGRectMake(//set rect frame)];
        moviePlayer.controlStyle =  MPMovieControlStyleDefault; 
        moviePlayer.shouldAutoplay=YES;
        moviePlayer.repeatMode = NO;  
        [moviePlayer setFullscreen:YES animated:NO]; 
        [moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];


- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code


    }
}

【讨论】:

    【解决方案4】:

    尝试直接询问您的捆绑包,而不是手动设置文件路径

    NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"];
    

    【讨论】:

      【解决方案5】:

      在将播放器添加为当前视图的子视图后,使用我们要播放的视频的 URL(它可以是设备本地文件的路径或实时 URL。)初始化播放器。

      MPMoviePlayerController 类支持的视频格式如下

      1. .mov .mpv .3gp .mp4

      我不确定这篇文章对你有多大帮助。我是新来的。我按照 this article

      中提供的分步说明进行操作

      【讨论】:

        【解决方案6】:
        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
        

        希望能帮到你

        【讨论】:

          【解决方案7】:

          我花了几分钟来调试问题,但答案很简单。这是交易:

          • 如果您希望 MPMoviePlayerViewController 从 Web URL 播放,请使用以下命令: NSURL *url = [NSURL URLWithString:@"https://www.test.com/anyMovie.mp4"];

          • 如果您希望 MPMoviePlayerViewController 从 App Bundle 播放它,请使用以下命令: NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"anyMovie" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:moviePath];

          其余部分相同,只是您必须按如下方式设置此属性“movieSourceType”:

          MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
          moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
          [self presentViewController:moviePlayerView animated:YES completion:^{}];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-07-26
            • 2013-07-08
            • 2014-02-07
            • 1970-01-01
            • 2012-11-29
            • 2013-10-10
            • 2012-05-11
            相关资源
            最近更新 更多