简体   繁体   English

播放视频时,MPMoviePlayerViewController退出

[英]MPMoviePlayerViewController quits while playing the video

I am recording the video in my application. 我正在应用程序中录制视频。 I am saving that video inside Documents directory. 我将该视频保存在Documents目录中。 After taking the video if I try to play the video using the following code. 拍摄视频后,如果我尝试使用以下代码播放视频。 The player opens and quits in one seconds. 播放器打开并在一秒钟内退出。 However if I Quit and then open my app fresh the video I took earlier is playing. 但是,如果我退出,然后重新打开我的应用,则我正在播放的视频正在播放。

+ (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];  
}  


}

This is the code I use for storing the video inside documents. 这是我用于将视频存储在文档内部的代码。

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

I finally figured out the answer after few hours of searching. 经过几个小时的搜索,我终于找到了答案。 This is the code I am using now.. 这是我现在使用的代码。

  -(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];
}

the code moviePlayerController.useApplicationAudioSession = NO; 代码moviePlayerController.useApplicationAudioSession = NO; is used to solve this problem. 用于解决此问题。 I am not sure how this problem solved I guess I am using session for audio I guess that was causing problem to this. 我不确定这个问题是如何解决的,我想我正在使用音频会话,这可能是导致问题的原因。

Also the code which I originally posted was taking lots of memory like if I opened 2 video it cost 104 mb allocation for me. 同样,我最初发布的代码占用了大量内存,例如,如果我打开2个视频,则为我分配了104 mb的内存。 This new code was perfect and it is taking only less memory. 这段新代码非常完美,仅占用较少的内存。 Hope this will help for some one.. 希望这会有所帮助。

*NSURL fileURL = [NSURL fileURLWithPath:theURL]; * NSURL fileURL = [NSURL fileURLWithPath:theURL];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM