以下代码用于将视频保存到相册。
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
如果是视频,我想播放它,如果它是图像,我想显示它。
帮助我。
答案 0 :(得分: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];
}
答案 1 :(得分: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
编辑:大多数Cocoa项目现在都使用arc
,因此上述代码会要求您自己保留MPMoviePlayerController
个实例(如this answer中所述) )。