1
votes

I have looked at a dozen different posts here and on the web. Still can't figure out. I can play a movie locally that I have attached to supporting files but when I try to play movie picked from camera roll I get a black screen. So I believe the problem is in getting the url from the uipicker and playing it.

I think my problem lies here in first two lines of playMovie-

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

This is not giving me the proper url to play movie from picker. I have also tried putting 'url' instead of @"public.movie"

#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

1 Answers

0
votes

the problem is here

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

try this:

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

and when you play video instead this

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

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

and movieplayer should be a property. hope it helps.