尝试从相机胶卷播放电影时出现黑屏

时间:2013-01-09 23:21:27

标签: xcode url mpmovieplayercontroller playback

我在这里和网上看过十几个不同的帖子。还是想不通。我可以在本地播放我附加到支持文件的电影,但是当我尝试播放从相机胶卷中拾取的电影时,我会看到黑屏。所以我认为问题在于从uipicker获取url并播放它。

我认为我的问题出在前两行playMovie-

NSURL *videoURL = [NSURL URLWithString:@"public.movie"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL]; 

这不是给我一个正确的网址来从选择器播放电影。我也试过把'url'代替@“public.movi​​e”

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize player;

-(IBAction) selectMovie
{

    UIImagePickerController *picker =
    [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;


    picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

    [self presentModalViewController:picker animated:YES];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    //NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
    //[mediaType isEqualToString:@"public.movie"];
    //{
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];//used to be*videoURL
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];


    [picker dismissModalViewControllerAnimated:YES];
    //[picker release];

}

-(IBAction)playMovie
{

    NSURL *videoURL = [NSURL URLWithString:@"public.movie"];
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];
    [moviePlayer prepareToPlay];
    moviePlayer.view.frame = CGRectMake(100, 100, 200, 200);
[self.view addSubview:moviePlayer.view];
    moviePlayer.shouldAutoplay = NO;
    moviePlayer.view.backgroundColor = [UIColor grayColor];

[moviePlayer play];

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

}

-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
    MPMoviePlayerController *moviePlayer = [notification object];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];
    [self dismissModalViewControllerAnimated:YES];
    //[player autorelease];
    //[player release];
}

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    [moviePlayer release];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

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


@end

1 个答案:

答案 0 :(得分:0)

问题出在这里

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

试试这个:

videoURL = (NSURL*)[info objectForKey:@"UIImagePickerControllerMediaURL"];

当你播放视频而不是这个

NSURL *videoURL = [NSURL URLWithString:@"public.movie"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: videoURL];

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

和movieplayer应该是一个属性。 希望能帮助到你。

相关问题