mpmovieplayercontroller一启动就会消失

时间:2012-06-25 09:53:44

标签: xcode mpmovieplayercontroller

当我点击“播放电影”时,mpmovieplayer呈现自己半秒钟,我看到它正在加载电影并且控件在那里,但随后它返回主屏幕而不播放视频。

我该如何解决这个问题?

编辑: 我已经更改了代码,现在播放器仍然保持正常状态但仍然无法加载,它不会播放电影。

问题是:没有为电影准备电影?或者不在后台停止电影,然后重新启动它?

   #import "ViewController.h"


@implementation ViewController
@synthesize moviePlayer;

-(IBAction)grabVid:(id)sender;
{
    [self presentModalViewController:imagePicker animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [imagePicker setDelegate:self];

    imagePicker = [[UIImagePickerController alloc] init]; 

    imagePicker.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType:
     imagePicker.sourceType];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:imagePicker animated:YES];


    {
        [imagePicker dismissModalViewControllerAnimated:YES];
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    //[imagePicker dismissModalViewControllerAnimated:YES];
}

-(IBAction)playMovie:(id)sender  
{  
    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    [self.view addSubview:playercontroller.moviePlayer.view];
    [playercontroller.moviePlayer setFullscreen:YES animated:YES];
    playercontroller.moviePlayer.shouldAutoplay = NO;
    [self presentMoviePlayerViewControllerAnimated:playercontroller];
    playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [playercontroller.moviePlayer play];  

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

    }  

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

    [moviePlayerController.view removeFromSuperview];  
    //[playercontroller.moviePlayer release];  
}  

- (void)dealloc {  
    //[super dealloc];  
}  




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

1 个答案:

答案 0 :(得分:0)

-(IBAction) playVideo

{
    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    moviePlayer.initialPlaybackTime = 0;

    [self presentMoviePlayerViewControllerAnimated:playercontroller];
    playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [playercontroller.moviePlayer play];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer];


    moviePlayer.controlStyle = MPMovieControlStyleDefault;
    moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];


  // playercontroller = nil;

}