Asynchronous MPMoviePlayerController load has no d

2019-05-10 05:19发布

I assume that the initWithContentURL: method is not asynchronous. I added the following methods to do the load in the background and then assign the temporary player to my variable.

-(void)createPlayer {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

MPMoviePlayerController *temp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"URLString"]];

[self performSelectorOnMainThread:@selector(passPlayer:) withObject:temp waitUntilDone:YES];

[temp release];
[pool release];
}

-(void)passPlayer:(MPMoviePlayerController*)temp {    
player = [temp retain];
player.movieSourceType = MPMovieSourceTypeStreaming;
//player.view.hidden = YES;
//[self.view addSubview:player.view];
[player prepareToPlay];
}

The problem is when I print out the duration of the player I get this:

double duration = player.duration;
NSLog(@"duration: %f, duration);

console output:  duration: nan

I have added the MPMoviePlayerControllerNotifications to be notified when there is a duration value available, but even then the value is still 'nan'. If I do the load on the main thread the duration has a value, but I don't want to use the main thread so my UI doesn't lock up.

Any ideas?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-05-10 05:40

This happens because the movie player has not been able to calculate the duration. If you subscribe to the MPMovieDurationAvailableNotification notification, you'll be notified if and when a value has been determined.

MPMovieDurationAvailableNotification

This notification is posted when the duration of a movie object is determined. The object of the notification is the MPMoviePlayerController object itself. There is no userInfo dictionary. The duration value is reflected in the duration property of the movie player controller.

Source: Apple documentation

查看更多
淡お忘
3楼-- · 2019-05-10 05:42

First of all, don't create UI objects on a non-main thread. All UI operations must be done on the main thread, or odd things will occur. Such as, perhaps, the movie never loading and NAN being returned for the duration.

Second, initWithContentURL: probably is asynchronous or there wouldn't be notifications to let you know when the movie was actually loaded. Just create the controller as you would normally, subscribe to the appropriate notifications to know when the status is updated, and then check the status immediately just in case the movie happened to be a local file or already cached so the load was able to happen synchronously.

查看更多
你好瞎i
4楼-- · 2019-05-10 05:47

From the official doc :

Discussion If the duration of the movie is not known, the value in this property is 0.0. If the duration is subsequently determined, this property is updated and a MPMovieDurationAvailableNotification notification is posted.

So use NSNotificationCenter to register the MPMovieDurationAvailableNotification notification.

查看更多
登录 后发表回答