几秒钟后视频播放消失

时间:2012-02-24 13:14:17

标签: ios video mpmovieplayercontroller

我想使用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];  
}

1 个答案:

答案 0 :(得分: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]; 

// .....
}