MPMoviePlayerController 无法加载滚动图库中的第一个视频

发布于 2024-12-06 12:14:50 字数 2200 浏览 0 评论 0原文

我正在 iOS SDK 4.3 上进行开发。 我有一个水平滚动的分页图库,可以显示来自远程源的图像或视频。对于分页视图,我使用公开可用的 ATPagingView:页面的重用与 TableViewCell 类似。但对于视频,我使用单个 MPMoviePlayerController,我将其 .view 属性作为子视图分配给多个页面(我知道,我知道...):

moviePlayerController = [[MPMoviePlayerController alloc] init];
moviePlayerController.repeatMode = MPMovieRepeatModeNone;
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
moviePlayerController.shouldAutoplay = false;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateChangeAction:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

您可以看到我已经注册了 MoviePlayer 事件 LoadState 通知。 当新页面出现在屏幕上时,我会根据需要开始加载视频:

- (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView
{
if (pagingView.currentPageIndex < 0)
    return;

NSLog(@"currentPageDidChangeInPagingView");
GalleryPageView *currentPage = (GalleryPageView *)[pagingView viewForPageAtIndex:pagingView.currentPageIndex];
if (![currentPage.gestureRecognizers count]) {
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetZoom)];
    recognizer.numberOfTapsRequired = 2;
    [currentPage.zoomView addGestureRecognizer:recognizer];
    recognizer.delegate = self;
    [recognizer release];
}

moviePlayButton.hidden = YES;

[activityView stopAnimating];
FeedItem *feedItem = (FeedItem *)[dataRoot objectAtIndex:pagingView.currentPageIndex];
if (feedItem.contentType == FeedContentTypeMovie) {
    moviePlayerController.contentURL = [NSURL URLWithString:feedItem.movieUrl];
    [moviePlayerController prepareToPlay];
    [activityView startAnimating];
}

现在,IFF 第一页面包含视频,即使在我调用 moviePlayerController.prepareToPlay 之后,它也不会加载:没有 loadState 事件被解雇了。以下页面按预期工作。 我尝试在 MPlayerController 初始化时预加载固定视频,但在这种情况下,固定视频已正确加载 (MPMovieLoadState == 3),而第一页上的视频会导致 MPMovieLoadStateUnknown 发生更改。

电影网址正确。 当我从第 2 页滚动回第 1 页时,第一个视频已加载 (MPMovieLoadState == 3),但没有显示。

你建议调查什么?共享视图架构有那么可怕吗?毕竟,它适用于以下页面。 prepareToPlay 是否有任何已知的奇怪行为?例如,如果视图被任何人“虐待”,MPC 是否可能会生气,然后拒绝加载内容?否则您会如何解释 MPMovieLoadStateUnknown?我很确定没有其他 MPC 幽灵实例在搞乱(我读过这可能是一个问题)。

谢谢你,也很抱歉发了这么长的帖子。

I'm developing on iOS SDK 4.3.
I have an horizontally-scrolling paged gallery that can display images or videos from a remote feed. For the paged views I'm using the publicly available ATPagingView: pages are reused similarly to TableViewCells. But for the videos I'm using a single MPMoviePlayerController, whose .view property I assign to the several pages as a subview (I know, I know...):

moviePlayerController = [[MPMoviePlayerController alloc] init];
moviePlayerController.repeatMode = MPMovieRepeatModeNone;
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
moviePlayerController.shouldAutoplay = false;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieLoadStateChangeAction:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

You can see I've registered for MoviePlayer event LoadState notifications.
When a new page goes onscreen, I start loading a video if needed:

- (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView
{
if (pagingView.currentPageIndex < 0)
    return;

NSLog(@"currentPageDidChangeInPagingView");
GalleryPageView *currentPage = (GalleryPageView *)[pagingView viewForPageAtIndex:pagingView.currentPageIndex];
if (![currentPage.gestureRecognizers count]) {
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetZoom)];
    recognizer.numberOfTapsRequired = 2;
    [currentPage.zoomView addGestureRecognizer:recognizer];
    recognizer.delegate = self;
    [recognizer release];
}

moviePlayButton.hidden = YES;

[activityView stopAnimating];
FeedItem *feedItem = (FeedItem *)[dataRoot objectAtIndex:pagingView.currentPageIndex];
if (feedItem.contentType == FeedContentTypeMovie) {
    moviePlayerController.contentURL = [NSURL URLWithString:feedItem.movieUrl];
    [moviePlayerController prepareToPlay];
    [activityView startAnimating];
}

Now, IFF the first page contains a video, even after I call moviePlayerController.prepareToPlay, it doesn't load: no loadState event is fired. Following pages instead work as expected.
I've tried to pre-load a fixed video on MPlayerController initialization, but in that case the fixed video is correctly loaded (MPMovieLoadState == 3), while video on first page causes a change to MPMovieLoadStateUnknown.

The movie URL is correct.
When I scroll back from page 2 to page 1, first video is loaded (MPMovieLoadState == 3), but it doesn't show.

What do you suggest investigating? Is the shared-view architecture so horrible? It works for the following pages, after all. Is there any known weird behavior by prepareToPlay? For example, is it possible MPC gets angry if the view is "mistreated" by anyone, and then it refuses to load stuff? How else would you explain MPMovieLoadStateUnknown? I'm pretty sure there is no other ghost instance of MPC messing around (I've read it could be a problem).

Thank you and sorry for the long post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苯莒 2024-12-13 12:14:50

由于我的代码中存在与此非常相似的内容,我最终不得不使用 AVPlayer。

I had to end up using AVPlayer because of something very similar to this in my code.

庆幸我还是我 2024-12-13 12:14:50

我也有同样的问题。在 iOS7 中不会发生这种情况,但在 iOS 6 中 loadState 为 3,这在以下情况中没有考虑到:

enum {
    MPMovieLoadStateUnknown        = 0,
    MPMovieLoadStatePlayable       = 1 << 0,
    MPMovieLoadStatePlaythroughOK  = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES
    MPMovieLoadStateStalled        = 1 << 2, // Playback will be automatically paused in this state, if started
};
typedef NSInteger MPMovieLoadState;

我将 autoplay 设置为 NO 并使用prepareToPlay 等待通知播放,因此当收到 MPMoviePlayerLoadStateDidChangeNotification 时,在玩之前,我会像这样检查 loadState:

if (self.moviePlayerC.loadState & MPMovieLoadStatePlayable || self.moviePlayerC.loadState & MPMovieLoadStatePlaythroughOK)

也许在 iOS6 中,loadState 是一个可以同时包含多个状态的位值。

I had the same problem. In iOS7 this does not happen, but in iOS 6 loadState is 3, which is not contemplated in:

enum {
    MPMovieLoadStateUnknown        = 0,
    MPMovieLoadStatePlayable       = 1 << 0,
    MPMovieLoadStatePlaythroughOK  = 1 << 1, // Playback will be automatically started in this state when shouldAutoplay is YES
    MPMovieLoadStateStalled        = 1 << 2, // Playback will be automatically paused in this state, if started
};
typedef NSInteger MPMovieLoadState;

I had autoplay set to NO and use prepareToPlay waiting for the notifications to play, so when MPMoviePlayerLoadStateDidChangeNotification is received, before playing, I check the loadState like this:

if (self.moviePlayerC.loadState & MPMovieLoadStatePlayable || self.moviePlayerC.loadState & MPMovieLoadStatePlaythroughOK)

Maybe in iOS6 loadState is a bitWise value that can contains several states at the same time.

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