异步 MPMoviePlayerController 加载没有持续时间值

发布于 11-16 02:45 字数 992 浏览 3 评论 0原文

我假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败2024-11-23 02:45:29

首先,不要在非主线程上创建 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.

酸甜透明夹心2024-11-23 02:45:29

发生这种情况是因为电影播放器​​无法计算持续时间。如果您订阅 MPMovieDurationAvailableNotification 通知,您将在确定值时收到通知。

MPMovieDurationAvailableNotification

此通知将在以下情况下发布:
电影对象的持续时间是
决定。该对象的
通知是
MPMoviePlayerController 对象本身。
没有 userInfo 字典。这
持续时间值反映在
电影播放器​​的持续时间属性
控制器。

来源:苹果文档

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

蓝颜夕2024-11-23 02:45:29

来自官方文档:

讨论
如果电影的持续时间未知,则此属性中的值为 0.0。如果随后确定持续时间,则更新此属性并发布 MPMovieDurationAvailableNotification 通知。

因此,使用 NSNotificationCenter 注册 MPMovieDurationAvailableNotification 通知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文