问题
I have been trying to get a video to play using MPMoviePlayerController for about a week with very little success! I have finally got it to the stage where it loads the video (or at least I presume it does as it can tell me the video duration).
However, my video never becomes playable therefore I am just presented with a black square.
My code is as follows:
- (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];
}
I am printing out a lot of the values to NSLog just to check them, so thought I should provide them below too just to give a better idea of what I'm seeing:
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
I did setup a notification to check for MPMoviePlayerLoadStateDidChangeNotification but it never changes. Has anyone had a similar issue, or know what could be wrong with my code? If you need anymore information feel free to ask.
EDIT 1 (Testing danh's code)
When using the following:
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];
My logs are:
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
So it seems to be the same as the original code where moviePlayerLoadStateChanged is never called. However, I'm still confused as to how the MPMoviePlayerController can pick up the duration of the video if it is unable to load it? Could this be a codec issue? Is there any way to check whether or not the video will work at all after downloaded?
EDIT 2:
Okay this looks like a possible codec issue. I have now tried it with 2 similar URLs, 1 mov / 1 3gp, and the mov one will play with sound but no video whilst the 3gp briefly shows "loading" on the video screen but never changes to the loaded state when this disappears. Can anyone suggest a way of getting this working, or another movie player with more types supported?
回答1:
Try replacing all of your setup code with this:
// 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];
Log state changes in moviePlayerLoadStateChanged:. If that doesn't work, it's probably the url that's the problem.
来源:https://stackoverflow.com/questions/14199159/mpmovieplayer-loading-the-video-but-never-enters-readytoplay-state