无法使用MPMoviePlayerViewController播放视频

8
我使用以下ViewController.m创建了一个新项目。当我运行应用程序时,我可以看到一个期望的起点/大小(38, 100, 250, 163)的框,但它是黑色的,没有视频播放。在Xcode中有一个奇怪的输出:
2012-08-23 15:36:45.559 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-08-23 15:36:45.560 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay
2012-08-23 15:37:18.186 VideoTest1[11398:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)

请注意,该视频是使用Videora iPhone转换器转换的,并且在Xcode中播放正常(因此不是视频问题);视频路径正确,因为当我指定demo-iPhone1(不存在)时,我会收到nil异常。我在模拟器和iPhone上尝试过:始终是黑色框。有什么想法吗?
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>  

@interface ViewController ()

@end

@implementation ViewController
- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [moviePlayerController.view setFrame:CGRectMake(38,
                                                    100,
                                                    250,
                                                    163)];
    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

你尝试过其他视频吗?比如从 Vimeo 下载的 MP4 文件? - Steve Moser
Steve,视频真的没问题,因为它在我的另一个应用程序中使用完全相同的代码播放。但仍然无法弄清楚区别。顺便说一下,我添加了Xcode输出。也许它会给你一些关于问题是什么的想法。 - maxgrinev
4个回答

13

我刚刚用这行代码解决了一个类似的问题。现在播放器控制器显示出来了,视频也完美地播放了:

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
//
@synthesize moviePlayer = _moviePlayer;
//
[self.moviePlayer prepareToPlay];

根据您的环境进行修改。


2
在iPad/iOS6上调用“prepareToPlay”解决了我的问题。没有它,“MPMoviePlayerLoadStateDidChangeNotification”不会被发送,因为播放器不会预加载视频。 - diachedelic
1
这个修复对我也有效,之前在iOS6上出现了黑屏问题(而在iOS5及以下版本上正常运行)。 - Dado
这个解决了我的问题,但是当在5秒内从splitView中加载另一个视频时,我仍然可以听到之前视频的音频,直到5秒结束...有人遇到过这个问题吗?我正在考虑添加AVFoundation框架,以便我可以尝试控制音频会话。 - whyoz

3

我通过添加playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;来解决类似的问题。


2

您是否使用ARC?如果是,您必须保留MPMoviePlayerController!

请将以下内容添加到您的界面中:

@property (nonatomic, strong) MPMoviePlayerController *controller;

请检查viewDidLoad的最后一行

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                           name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];
    [moviePlayerController.view setFrame:CGRectMake(38,
                                                100,
                                                250,
                                                163)];
    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
    [self setController:moviePlayerController];
}

太多的教程展示了使用临时变量的用法。+1 以保留原意。 - DanneManne

-2

还可以看看视频格式。我在我的样本视频.m4v上一直遇到这个错误(它是从苹果网站下载的)。最后我尝试了一个不同的视频剪辑,它是.mp4格式的,它可以正常工作。我的控制台上仍然弹出了许多错误信息。

2013-01-22 15:44:04.850 VideoTesting[4497:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-22 15:44:04.851 VideoTesting[4497:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay
2013-01-22 15:44:04.861 VideoTesting[4497:c07] [MPAVController] Autoplay: Enabling autoplay

视频仍在播放。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接