MPMoviePlayerViewController在播放视频时退出

时间:2011-10-08 18:32:17

标签: iphone objective-c video

我正在我的应用程序中录制视频。我将该视频保存在Documents目录中。拍摄视频后如果我尝试使用以下代码播放视频。玩家在一秒钟内打开并退出。但是,如果我退出然后打开我的应用程序,我之前拍摄的视频正在播放。

+ (void) playMovieAtURL: (NSURL*) theURL :(id)sender{
NSLog(@"playMovieAtURL");
//senderID = (id *)sender;

if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
    NSLog(@"> 3.2");
    MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];

    if (mp)
    {
        // save the movie player object
        //self.moviePlayerViewController = mp;
        //[mp release];
        [sender presentMoviePlayerViewControllerAnimated:mp];
        //mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
        mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
        [mp.moviePlayer play];
    }
    [mp release];

} 
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
    NSLog(@"< 3.2");

    MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];

    theMovie.scalingMode = MPMovieScalingModeAspectFill;

    // Register for the playback finished notification
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector: @selector(myMovieFinishedCallback:)
     name: MPMoviePlayerPlaybackDidFinishNotification
     object: theMovie];

    // Movie playback is asynchronous, so this method returns immediately.
    [theMovie play];
    [theMovie release];


}

}

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

// If the moviePlayer.view was added to the view, it needs to be removed  
if ([aMoviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
    [aMoviePlayer.view removeFromSuperview];  
}  


}

这是我用于将视频存储在文档中的代码。

    NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
    NSData *webData = [NSData dataWithContentsOfURL:videoURL];
    self.itsVideoName = fileName;
    [webData writeToFile:[NSString stringWithFormat:@"%@/%@",dataPath,fileName] atomically:TRUE];

2 个答案:

答案 0 :(得分:1)

经过几个小时的搜索,我终于找到了答案。这是我现在使用的代码..

  -(IBAction)playMovie:(NSString *) theURL 
{
NSURL    *fileURL    =   [NSURL fileURLWithPath:theURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];

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

[self.view addSubview:moviePlayerController.view];
//After putting the following line the problem solved.
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}

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

[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

代码moviePlayerController.useApplicationAudioSession = NO;用来解决这个问题。我不知道这个问题是如何解决的,我想我正在使用会话音频,我想这会引起问题。

另外,我最初发布的代码占用了大量内存,就像我打开2个视频一样,它为我分配了104 MB。这个新代码很完美,只占用较少的内存。希望这对某些人有所帮助..

答案 1 :(得分:0)

* NSURL fileURL = [NSURL fileURLWithPath:theURL];

应该使用NSURL * fileURL = [NSURL URLWithString:theURL];