几秒钟后视频播放消失

gef*_*rce 3 video mpmovieplayercontroller ios

我想使用MPMoviePlayerController在UIView中播放视频.按下UIButton后,视频出现并开始播放.不幸的是,3-4秒后,视频变黑,音频仍在播放.有任何想法吗?谢谢你所有的时间.

(使用Xcode 4.2.1)

    -(void) playMovieButtonPressed:(UIButton*) button{

        NSString* video = @"testmovie.m4v";

        NSString *filepath   =   [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
        NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
        MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 


        [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(moviePlaybackComplete:)  
                                                   name:MPMoviePlayerPlaybackDidFinishNotification  
                                                   object:player];  

        player.movieSourceType = MPMovieSourceTypeFile;
        [player.view setFrame:CGRectMake(20, 400, 300, 200)];
        [self.view addSubview:player.view];
        [player prepareToPlay];
        [player play];  
    }

- (void) moviePlaybackComplete:(NSNotification*) notification {
    NSLog(@"videoviewcontroller complete:  %@", notification);

    MPMoviePlayerController *mymoviePlayerController = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
                                                  object:mymoviePlayerController];  
}
Run Code Online (Sandbox Code Playgroud)

gef*_*rce 7

我和一位Apple工程师谈过了.解决方案是播放器必须是视图控制器的实例变量或属性.所以当视图被拆除时它仍然可以访问.

-(void) playMovieButtonPressed:(UIButton*) button{

        NSString* video = @"testmovie.m4v";

        NSString *filepath   =   [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
        NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];  
        self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

// .....
}
Run Code Online (Sandbox Code Playgroud)