选择视频和播放时出现问题

时间:2013-07-02 11:23:50

标签: iphone ios mpmovieplayercontroller

我正试图从lib播放视频,但我无法弄清楚我做错了什么我尝试了很多东西而且它没有显示任何类型的错误我的代码在下面

- (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];
}

首先presetModelViewController显示的是什么然后我可以显示我的lib中的视频我可以选择其中一个但是当我选择视频时它会显示我的灰色视图而没有别的我的视频是没玩。

如果您在我的代码中发现任何错误并且可以帮助我,那么这将是非常有用的。

2 个答案:

答案 0 :(得分:1)

请通过以下链接来帮助您解决现有问题和实施问题。

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];

答案 1 :(得分:0)

This is what solved my problem thanks to spider1983 

   - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {

        [self dismissModalViewControllerAnimated: YES];
    }

    // For responding to the user accepting a newly-captured picture or movie
    - (void) imagePickerController: (UIImagePickerController *) picker
     didFinishPickingMediaWithInfo: (NSDictionary *) info {

        NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];


        [self dismissModalViewControllerAnimated:NO];

        // Handle a movie capture
        if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0)
            == kCFCompareEqualTo) {

            NSString *moviePath = [[info objectForKey:
                                    UIImagePickerControllerMediaURL] path];




            NSLog(@"%@",[info objectForKey:
                         UIImagePickerControllerMediaURL]);


        /* Create a new movie player object. */




            MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[info objectForKey:UIImagePickerControllerMediaURL]];



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


            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 prepareToPlay];
            [player setCurrentPlaybackRate:0.5];
            [player play];
        }







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


    }