0

我一直在尝试使用 MPMoviePlayerController 播放视频大约一周,但收效甚微!我终于把它带到了加载视频的阶段(或者至少我认为它可以告诉我视频持续时间)。

但是,我的视频永远无法播放,因此我只看到了一个黑色方块。

我的代码如下:

  - (void)loadMovieWithURL:(NSURL *)movieURL
{
    NSLog( @"Video URL = '%@'", [movieURL path]);
    NSData *data = [NSData dataWithContentsOfURL:movieURL];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayerPlaybackStateDidChange:)  name:MPMoviePlayerPlaybackStateDidChangeNotification  object:nil];

    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] init];
    self.moviePlayer = mp;
    [self.moviePlayer.view setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.moviePlayer.view];
    [self.moviePlayer setContentURL:movieURL];
    self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

    [self.moviePlayer setFullscreen:NO];
    [self.moviePlayer setScalingMode:MPMovieScalingModeFill];
    [self.moviePlayer setControlStyle:MPMovieControlStyleNone];

    //[self.moviePlayer setShouldAutoplay:YES];

    [self.moviePlayer prepareToPlay];
    [self.moviePlayer play];

}

我将很多值打印到 NSLog 只是为了检查它们,所以我想我也应该在下面提供它们,以便更好地了解我所看到的内容:

2013-01-07 15:33:39.858 WildMap_iOS[9887:16703] Video URL = '/Users/elliott/Library/Application Support/iPhone Simulator/6.0/Applications/A233395A-3E8F-4E35-BF42-09D9B743EA33/test.3gp'
    2013-01-07 15:33:40.100 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Disabling autoplay for pause
    2013-01-07 15:33:40.101 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Disabling autoplay
    2013-01-07 15:33:40.176 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Enabling autoplay
    2013-01-07 15:33:40.179 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
    2013-01-07 15:33:40.180 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
    2013-01-07 15:33:40.181 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
    2013-01-07 15:33:40.182 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
    2013-01-07 15:33:40.259 WildMap_iOS[9887:16703] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
    2013-01-07 15:33:40.401 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Enabling autoplay
    2013-01-07 15:33:41.344 WildMap_iOS[9887:16703] Is ready to display 0
    2013-01-07 15:33:41.344 WildMap_iOS[9887:16703] Duration: 45.267000

我确实设置了一个通知来检查 MPMoviePlayerLoadStateDidChangeNotification 但它永远不会改变。有没有人遇到过类似的问题,或者知道我的代码有什么问题?如果您需要更多信息,请随时询问。

编辑 1(测试 danh 的代码)

使用以下内容时:

    NSLog( @"Video URL = '%@'", [movieURL path]);
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.moviePlayer.view.frame = self.bounds;
[self addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];

//NSData *data = [NSData dataWithContentsOfURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(moviePlayerPlaybackStateDidChange:)  name:MPMoviePlayerPlaybackStateDidChangeNotification  object:nil];

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

我的日志是:

2013-01-08 12:08:16.105 WildMap_iOS[705:16703] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-08 12:08:16.105 WildMap_iOS[705:16703] [MPAVController] Autoplay: Disabling autoplay
2013-01-08 12:08:16.138 WildMap_iOS[705:16703] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-08 12:08:16.144 WildMap_iOS[705:16703] [MPAVController] Autoplay: Enabling autoplay
2013-01-08 12:08:16.153 WildMap_iOS[705:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-08 12:08:16.153 WildMap_iOS[705:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-08 12:08:16.157 WildMap_iOS[705:16703] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-08 12:08:16.177 WildMap_iOS[705:16703] [MPAVController] Autoplay: Enabling autoplay
2013-01-08 12:08:17.244 WildMap_iOS[705:16703] Is ready to display 0
2013-01-08 12:08:17.244 WildMap_iOS[705:16703] Duration: 45.267000

所以它似乎与从不调用moviePlayerLoadStateChanged的原始代码相同。但是,我仍然对 MPMoviePlayerController 无法加载视频时如何获取视频的持续时间感到困惑?这可能是编解码器问题吗?有什么方法可以检查视频下载后是否可以正常播放?

编辑2:

好的,这看起来像是一个可能的编解码器问题。我现在用 2 个类似的 URL 进行了尝试,1 mov / 1 3gp,mov 一个会播放声音但没有视频,而 3gp 会在视频屏幕上短暂显示“正在加载”,但当它消失时永远不会变为加载状态。任何人都可以建议一种使其正常工作的方法,或其他支持更多类型的电影播放器​​吗?

4

1 回答 1

0

尝试用以下代码替换所有设置代码:

// get the url as moveURL
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.moviePlayer.view.frame = self.view.bounds;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer prepareToPlay];

// notice what notification we subscribe to...
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayerLoadStateChanged:)
                                             name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

在 moviePlayerLoadStateChanged: 中记录状态更改。如果这不起作用,则可能是 url 的问题。

于 2013-01-07T15:50:04.363 回答