0

当我单击“播放电影”时,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
4

1 回答 1

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;

}
于 2012-06-25T09:58:03.737 回答