【问题标题】:MPMoviePlayer not removing from superviewMPMoviePlayer 未从超级视图中删除
【发布时间】:2012-07-06 09:25:50
【问题描述】:

我正在使用以下代码在 MPMoviePlayerController 中播放视频文件

  NSString* filePath = [[NSBundle mainBundle] pathForResource:@"one" ofType:@"mp4"];
    NSURL*  url = [NSURL fileURLWithPath:filePath];

    _movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [_movie.view setFrame:self.view.bounds];
    [self.view addSubview:_movie.view];
    _movie.fullscreen=YES;
    _movie.controlStyle=MPMovieControlStyleFullscreen;
    [_movie prepareToPlay];
    [_movie play];

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close:)name:MPMoviePlayerPlaybackDidFinishNotification object:_movie];

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

    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];

    if(reason == MPMoviePlaybackStateStopped) {

        NSLog(@"Stop");

    }
    else if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Playback Ended ");
    }
    else if (reason == MPMovieFinishReasonUserExited) {

        NSLog(@"Exited");
        [_movie.view removeFromSuperview];
    }
    else if (reason == MPMovieFinishReasonPlaybackError) {
        //error
        NSLog(@"Error");
    }    
}

我能够收到通知,并且 Movieplayer 没有从超级视图中删除。

可能是什么问题??

【问题讨论】:

  • 你想只删除 userwillexited 中的 mpmovieplayercontroller 吗?
  • 是的,没错。我的意思是当按下完成按钮时。
  • 现在,如果我点击完成按钮,电影就消失了(屏幕是黑色的)。但控件仍然存在。

标签: ios xcode cocoa mpmovieplayercontroller mpmovieplayer


【解决方案1】:

按照说明试试这个:

当我收听 MPMoviePlayerWillExitFullscreenNotification 时。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(doneButtonClick:) 
                                             name:MPMoviePlayerWillExitFullscreenNotification 
                                           object:_movie];

和选择器方法:

-(void)doneButtonClick:(NSNotification*)aNotification{
  [_movie.view removeFromSuperview];
}

(或)

在下面的教程中使用 mpmovieplayerviewcontroller 的更好方法

http://mobiledevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

【讨论】:

  • 当点击完成按钮时,它没有为我调用 MPMoviePlayerWillExitFullscreenNotification,它正在调用 MPMoviePlayerPlaybackDidFinishNotification
  • 试试这个通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerWillExitFullscreenNotification object:_movie];
  • 我已经试过了,没有为控件样式 MPMovieControlStyleFullscreen 发送通知。
  • 如果 (reason == MPMovieFinishReasonUserExited) 在您的代码中被调用或未调用?
  • 你能再告诉我一件事吗?如果我将控件样式用作 MPMovieControlStyleDefault ,我们将拥有完成按钮和双头箭头。如果我按下完成按钮,它将从超级视图中删除。如果我按双箭头(靠近前进按钮),它也会从超级视图中删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多