MPMoviePlayer 不会在 [moviePlayer.view removeFromSuperview] 和 [moviePlayer release] 上消失

发布于 2024-11-17 09:19:53 字数 2822 浏览 9 评论 0原文

我的 MPMoviePlayerController 有问题。当我观看视频并点击左上角的“完成”按钮时,MoviePlayer 不会消失,即使代码似乎被称为:

   NSURL *url = [NSURL URLWithString:article.enclosureLink];    

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  

    // Set movie player layout
    [moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [moviePlayer setFullscreen:YES];

    // May help to reduce latency
    [moviePlayer prepareToPlay];

    // Register to receive a notification when the movie has finished playing.      
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlayBackDidFinish:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayer];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(movieReadyToPlay:)  
                                                     name:MPMoviePlayerLoadStateDidChangeNotification  
                                                   object:moviePlayer];

选择器:

- (void) movieReadyToPlay:(NSNotification*)notification {
    MPMoviePlayerController *moviePlayer = [notification object];  

    if(moviePlayer.loadState == MPMovieLoadStatePlayable){
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];         
        moviePlayer.controlStyle = MPMovieControlStyleFullscreen;  
        //moviePlayer.shouldAutoplay = YES; 
        [self.view addSubview:moviePlayer.view];  
        [moviePlayer setFullscreen:YES animated:YES];  
        [moviePlayer play];
    }

}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {  
    MPMoviePlayerController *moviePlayer = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];  

    [moviePlayer setFullscreen:NO animated:YES];  
    [moviePlayer.view removeFromSuperview];  
    [moviePlayer release];

    NSLog(@"Finished movie!");
} 

在我看来,这是一个非常简单的代码,但我一定犯了一个愚蠢的错误。 NSLog显示该函数被调用,但玩家停留在原处,没有办法摆脱它。

另外,该播放器在所谓的发布后仍在运行这一事实似乎表明存在根本性错误,我只是不明白是什么。

有人有建议吗?

[更新:] 奇怪的是,在 iPhone 模拟器中它运行得很好!

[更新2:] 我尝试并创建了一个特定的 UIviewcontroller,尽管这不是我想要的方式,因为动画不太好。但我了解到我也有同样的问题。似乎必须做点什么来解雇玩家,但它又重新开始了。

当我把 [self.movi​​ePlayer setFullscreen:YESanimated:YES];在viewDidApear中,然后单击播放器中的“完成”按钮,当我点击“完成”按钮时,播放器视频会重新开始(再次调用viewDidAppear)。所以在我看来,有些东西被触发了,让视频重新开始。

如果我把它放在 viewDidLoad 中,那么系统就可以工作,但是图形是混合和混乱的......

任何帮助都非常非常感谢,因为我现在花了两天时间在这上面,而没有搞清楚它!

I have a problem with MPMoviePlayerController.When I am watching a video, and hit the 'Done' button on the left top, the MoviePlayer does not disappear, even though the code seems to be called:

   NSURL *url = [NSURL URLWithString:article.enclosureLink];    

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  

    // Set movie player layout
    [moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [moviePlayer setFullscreen:YES];

    // May help to reduce latency
    [moviePlayer prepareToPlay];

    // Register to receive a notification when the movie has finished playing.      
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlayBackDidFinish:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayer];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(movieReadyToPlay:)  
                                                     name:MPMoviePlayerLoadStateDidChangeNotification  
                                                   object:moviePlayer];

And the selectors:

- (void) movieReadyToPlay:(NSNotification*)notification {
    MPMoviePlayerController *moviePlayer = [notification object];  

    if(moviePlayer.loadState == MPMovieLoadStatePlayable){
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];         
        moviePlayer.controlStyle = MPMovieControlStyleFullscreen;  
        //moviePlayer.shouldAutoplay = YES; 
        [self.view addSubview:moviePlayer.view];  
        [moviePlayer setFullscreen:YES animated:YES];  
        [moviePlayer play];
    }

}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {  
    MPMoviePlayerController *moviePlayer = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];  

    [moviePlayer setFullscreen:NO animated:YES];  
    [moviePlayer.view removeFromSuperview];  
    [moviePlayer release];

    NSLog(@"Finished movie!");
} 

This looks to me a very straight forward code, but I must make a stupid mistake. The NSLog shows that the function is called, but the player stays where it is and there is no way of getting rid of it.

Also, the very fact that the player is still operational after the alleged release seems to indicate that there is something fundamental wrong, I just don't see what.

Is there anybody who has a suggestion?

[Update:]
Strangely in the iPhone Simulator it works fine!

[Update2:]
I tried and created a specific UIviewcontroller, even though it is not the way I want to do it as the animations are not nice. But what I learned is that I have the same problem. It seems to hav to do something with dismissing the player, but it starting again.

When I put [self.moviePlayer setFullscreen:YES animated:YES]; in viewDidApear, and click the 'Done' button in the player, the player, the video starts over again when I hit the Done button (the viewDidAppear is called again). So something is triggered, so it seems to me, to make the video start again.

If I put it viewDidLoad, then the system works, but the graphics are mixed and confused...

Any help is really, really appreciated as I spend two days on this now without making head or tail of it!

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

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

发布评论

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

评论(2

沐歌 2024-11-24 09:19:54

对我来说,我尝试了所有这些:
<代码>
[电影播放器​​停止];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;

但没有任何效果。我发现这与我的 MPMoviePlayerController 进入全屏有关。修复?

        [moviePlayer setFullscreen:NO animated:YES];

For me, I tried all of these:

[moviePlayer stop];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;

And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?

        [moviePlayer setFullscreen:NO animated:YES];
走走停停 2024-11-24 09:19:54

添加

[moviePlayer stop]  

在之前

 [moviePlayer.view removeFromSuperview]

可能会起作用。

更新
如果这不起作用,请在删除子视图之前尝试将 controlstyle 设置为 MPMovieControlStyleNone。大多数情况下,controlStyle 会导致此类问题。

Adding

[moviePlayer stop]  

before

 [moviePlayer.view removeFromSuperview]

may work.

Update:
If this doesn't work then try setting controlstyle to MPMovieControlStyleNone before removing the subview.Most of the time the controlStyle causes such problems.

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