0

我正在尝试从 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是显示然后我可以显示我的库中的视频我可以选择其中一个但是当我选择视频时它显示我的灰色视图而没有其他任何内容并且我的视频没有播放。

如果你能在我的代码中发现任何错误并且可以帮助我,那么这将是很大的帮助。

4

2 回答 2

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];
于 2013-07-02T11:31:23.080 回答
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];
        }


    }
于 2013-07-02T12:09:00.197 回答