Search code examples
iphoneiosmpmovieplayercontroller

issue in picking video and playing it


i being trying to play video from lib but i cant figure out what im doing wrong i have tried many things and its not showing any kind of error my code is below

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

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    NSURL *url = [[NSURL alloc]initWithString:mediaType];

    NSLog(@"%@",url);

    [self dismissModalViewControllerAnimated:NO];


    /* Create a new movie player object. */
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];

    if (player)
    {
               /* Specify the URL that points to the movie file. */
        [player setContentURL:url];





        /* Add a background view as a subview to hide our other view controls
         underneath during movie playback. */


        CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                            kMovieViewOffsetX,
                                            kMovieViewOffsetY );
        /* Inset the movie frame in the parent view frame. */
        [[player view] setFrame:viewInsetRect];

        [player view].backgroundColor = [UIColor lightGrayColor];

        /* To present a movie in your application, incorporate the view contained
         in a movie player’s view property into your application’s view hierarchy.
         Be sure to size the frame correctly. */
        [self.view addSubview: [player view]];


        [player setCurrentPlaybackRate:0.5];
        [player play];
    }







        // Register for the playback finished notification
        [[NSNotificationCenter defaultCenter]
         addObserver: self
         selector: @selector(myMovieFinishedCallback:)
         name: MPMoviePlayerPlaybackDidFinishNotification
         object: player];



}



// When the movie is done, release the controller.
-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
    MPMoviePlayerController *playerr = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:playerr];
    [playerr stop];
    [self.view removeFromSuperview];
    [playerr autorelease];
}

what happen is first presetModelViewController is getting displayed then i can shows videos which are in my lib i can select one of them but when i choose video then it shows my grey color view and nothing else and my video is not getting played.

If you could find any errors in my code and can help me with this then it will be great help.


Solution

  • Please go through the below link will help you with your existing issue and also implementation.

    http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios

     NSURL *videoURL = [NSURL URLWithString:videoURLString]; 
     MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]   
     initWithContentURL:videoURL]; 
     [moviePlayer prepareToPlay]; 
      [moviePlayer play]; 
      [self.view addSubview:moviePlayer.view];