2012-04-04 65 views
1

以下代码用于保存视频到相册。如何访问相册中的视频并播放它们?

else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) 
    { 

     NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; 
     UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil); 
    } 

如果是视频我想播放它,如果它是图像我想要显示它。

帮帮我。

+0

尝试更新代码..! – Sarah 2012-04-04 08:03:38

回答

1

的代码检查,如果是图像或视频:

NSString *mediaType1 = [info objectForKey:UIImagePickerControllerMediaType]; 
    NSLog(@"mediaType : %@",mediaType1); 

    if ([mediaType1 isEqualToString:@"public.image"]) 
    { 
     //Show Image 
} 
else 
{ 
    //show Video 
} 

要播放视频检查URL

编辑:

 NSString *moviePath = [bundle pathForResource:@"IMG_0017" ofType:@"MOV"]; 
     NSLog(@"moviePath : %@",moviePath); 
    // NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; 

     NSURL *movieURL = [NSURL URLWithString:strValURL]; 
     NSLog(@"movieURL : %@",movieURL); 
     if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) 
     { 
      NSLog(@"> 3.2"); 
      MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; 
      if (mp) 
      { 
       [self presentMoviePlayerViewControllerAnimated:mp]; 
       mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
       [mp.moviePlayer play]; 
       [mp release]; 
      } 
     } 
     else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) 
     { 
      NSLog(@"< 3.2"); 
      theMovie = [[MPMoviePlayerController alloc] initWithContentURL: movieURL]; 
      theMovie.scalingMode = MPMovieScalingModeAspectFill; 
      [[NSNotificationCenter defaultCenter] 
      addObserver: self 
      selector: @selector(myMovieFinishedCallback:) 
      name: MPMoviePlayerPlaybackDidFinishNotification 
      object: theMovie]; 
      [theMovie play]; 
     } 

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{ 
    MPMoviePlayerViewController *moviePlayer = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:moviePlayer]; 
    [moviePlayer release]; 
    [self.navigationController setNavigationBarHidden:NO animated:YES]; 
} 

- (void) movieFinishedCallback:(NSNotification*) aNotification 
{ 
    MPMoviePlayerController *player = [aNotification object]; 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:MPMoviePlayerPlaybackDidFinishNotification 
    object:player]; 
    [player autorelease]; 
} 
+0

我知道如何检查它..我想如果它是视频如何发挥它 – Shreedhar 2012-04-04 07:05:34

+0

因为我给了网址。核实。 – Sarah 2012-04-04 07:18:41

1

在你- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法,你可以得到视频的网址和播放:

@property (nonatomic,retain) MPMoviePlayerController *moviePlayerController; 

if ([mediaType isEqualToString:@"public.movie"]) 
{ 
    NSURL *aURL = [info objectForKey:UIImagePickerControllerMediaURL];//get the url 
    // and init the video player using this url 
    _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:aURL]; 
    [self.view _moviePlayerController.view]; 
    _moviePlayerController.useApplicationAudioSession = NO; 
    _moviePlayerController.fullscreen = YES; 
    [_moviePlayerController play]; 
} 

当然,你必须导入MediaPlayer.framework

编辑:大多数可可项目现在使用arc,因此上面的代码将要求您自己保留MPMoviePlayerController实例(如this answer中所述)。

+0

它没有播放视频 – Shreedhar 2012-04-04 07:46:38

+0

@Shreedhar我已经在' - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info'方法中完成了这项工作。 – tipycalFlow 2012-04-04 08:20:43

+0

不起作用... – c0d3Junk13 2014-07-15 15:54:20

相关问题