我一直在尝试使用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在视频屏幕上短暂显示“加载”但是当它消失时从未改变到加载状态。任何人都可以建议一种方法来使这个工作,或支持更多类型的其他电影播放器?
答案 0 :(得分: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中记录状态更改:如果这不起作用,那可能是问题所在的网址。