Asynchronous MPMoviePlayerController load has no duration value

丶灬走出姿态 提交于 2020-01-02 08:08:49

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6397896/asynchronous-mpmovieplayercontroller-load-has-no-duration-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!