开发者

MPMoviePlayerViewController quits while playing the video

开发者 https://www.devze.com 2023-04-11 12:07 出处:网络
I am recording the video in my application.I am saving that video inside Documents directory.After taking the video if I try to play the video using the following code.The player opens and quits in on

I am recording the video in my application. I am saving that video inside Documents directory. 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 {  
MP开发者_如何学PythonMoviePlayerController *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; 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. This new code was perfect and it is taking only less memory. Hope this will help for some one..


*NSURL fileURL = [NSURL fileURLWithPath:theURL];

should use NSURL *fileURL = [NSURL URLWithString:theURL];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号