【问题标题】:MPMoviePlayerController does not remove view when clicking done单击完成时 MPMoviePlayerController 不会删除视图
【发布时间】:2012-03-05 09:38:35
【问题描述】:

我正在创建一个 MPMoviePlayerController 对象并以全屏模式流式传输视频。

我正在使用 UIViewController 来显示电影视图。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //http://www.youtube.com/watch?feature=player_detailpage&v=ebeQaznNcmE
    NSURL *url = [NSURL URLWithString:@"http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4"];
    MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    mPlayer.view.frame = gMainView.frame;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(moviePlayBackDidFinish:)
                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                            object:mPlayer];
    mPlayer.shouldAutoplay = YES;
    mPlayer.controlStyle = MPMovieControlStyleFullscreen;
    [gMainView addSubview:mPlayer.view];
    [mPlayer prepareToPlay];
    [mPlayer setFullscreen:YES animated:YES];
    [mPlayer play];
}


- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        //movie finished playing
    }
    else if (reason == MPMovieFinishReasonUserExited) {
        //user hit the done button
        MPMoviePlayerController *moviePlayer = [notification object];

        [[NSNotificationCenter defaultCenter] removeObserver:self      
                                                      name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
            [moviePlayer.view removeFromSuperview];
        }
        [moviePlayer release];
    }
    else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
    }
}

点击完成后,视频视觉对象会从屏幕上移除,但控件不会从屏幕上移除,视图也不会从屏幕上移除。

控件确实来到“//用户点击完成按钮”。它确实执行了从 superview 中删除视图的代码,我通过添加日志进行了检查,但是控件没有从屏幕上删除,视图也没有从屏幕上删除。 我做错了什么?

编辑:

如果我使用 MPMoviePlayerViewController,那么它甚至不会等我按下 Done。视频完成后,它会自动删除视图。但我不想那样。

编辑:

如果我删除“[mPlayer setFullscreen:YES animated:YES]”,那么当点击完成时,视图将被完全删除。但是视频没有全屏显示,状态栏变灰,这又是我不想要的。

【问题讨论】:

  • 你采取了很多步骤来描述你不想要的东西,但至少对我来说,这仍然不能真正说明你真正想要实现的目标。
  • 控件不会从屏幕上移除,播放器视图也不会从屏幕上移除。
  • 试试这个解决方案:stackoverflow.com/questions/6142571/…
  • @Anand 我遇到了与 MPmoviePlayerController 相同的问题。你有任何答案。如果请告诉我...谢谢

标签: iphone mpmovieplayercontroller


【解决方案1】:

下面的代码对我有用,希望对你也有帮助。

-(IBAction)playVedio:(id)sender{
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self presentMoviePlayerViewControllerAnimated:mp];

}

-(void)videoPlayBackDidFinish:(NSNotification*)notification  {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    [mp.moviePlayer stop];
    mp = nil;
    [mp release];
    [self dismissMoviePlayerViewControllerAnimated];  
}

【讨论】:

  • 请 Sirji 还添加更多代码,其他人可以轻松理解您的答案,您做了什么以及问题中的代码有什么问题,您做了什么来克服它,无论如何,谢谢解决方案:)
  • vl 下次小心点..!!:)
【解决方案2】:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    id presentedViewController = [window.rootViewController presentedViewController];
    NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

    if (window && [className isEqualToString:@"AVFullScreenViewController"]) {

        return UIInterfaceOrientationMaskAll;

    } else {

        UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

        if(UIInterfaceOrientationIsPortrait(interfaceOrientation))

        {

        }

        else if(UIInterfaceOrientationIsLandscape(interfaceOrientation))

        {


        }

        return UIInterfaceOrientationMaskPortrait;

        CGRect frame = [UIScreen mainScreen].applicationFrame;
        CGSize size = frame.size;
        NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",
                      UIInterfaceOrientationIsPortrait(interfaceOrientation) ? "Portrait" : "Landscape",
                      size.width, size.height]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多