异步 MPMoviePlayerController 加载没有持续时间值
我假设 initWithContentURL:
方法不是异步的。我添加了以下方法来在后台进行加载,然后将临时播放器分配给我的变量。
-(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];
}
问题是,当我打印出播放器的持续时间时,我得到以下信息:
double duration = player.duration;
NSLog(@"duration: %f, duration);
console output: duration: nan
我添加了 MPMoviePlayerControllerNotifications
以便在有可用持续时间值时收到通知,但即使如此,该值仍然是“nan”。如果我在主线程上进行加载,则持续时间会有一个值,但我不想使用主线程,因此我的 UI 不会锁定。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
首先,不要在非主线程上创建 UI 对象。所有UI操作都必须在主线程上完成,否则会出现奇怪的情况。例如,也许电影从未加载并且 NAN 在持续时间内被返回。
其次,
initWithContentURL:
可能是异步的,否则不会有通知让您知道电影何时实际加载。只需像平常一样创建控制器,订阅适当的通知以了解状态何时更新,然后立即检查状态,以防电影恰好是本地文件或已缓存,以便加载能够同步发生。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.