views:

2361

answers:

2

i want to detect if the phone is 3gs then record the video and save to documents directory.and then play movie by picking url from documents directory.note i do not want to save video to savedphotoalbum so that user not be able to delete video.also is there a way i can save video to database

+2  A: 

From The iPhone Application Guide:

Starting in iPhone OS 3.0, you can record video, with included audio, on supported devices. To display the video recording interface, create and push a UIImagePickerController object, just as for displaying the still-camera interface.

To record video, you must first check that the camera source type (UIImagePickerControllerSourceTypeCamera) is available and that the movie media type (kUTTypeMovie) is available for the camera. Depending on the media types you assign to the mediaTypes property, the picker can directly display the still camera or the video camera, or a selection interface that lets the user choose.

Using the UIImagePickerControllerDelegate protocol, register as a delegate of the image picker. Your delegate object receives a completed video recording by way of the imagePickerController:didFinishPickingMediaWithInfo: method.

On supported devices, you can also pick previously-recorded videos from a user’s photo library.

For more information on using the image picker class, see UIImagePickerController Class Reference. For information on trimming recorded videos, see UIVideoEditorController Class Reference and UIVideoEditorControllerDelegate Protocol Reference.

Once you have the video in your UIImagePickerController delegate you can then save it to your app's documents directory using standard file operations.

TechZen
can you post a sample code
Rahul Vyas
Apple sample code is available by following the links to the class references in the guide link I provided. The code for handling video is substantially the same as handling photos. It's simple. Don't make the mistake of assuming video is more complicated than photos.
TechZen
A: 

/* Saving the video / // Get the new unique filename NSDateFormatter adateFormatter=[[NSDateFormatter alloc]init]; [adateFormatter setDateFormat:@"dd-yyyy-h:mm:s"]; NSDate* atempdate=[adateFormatter dateFromString:[adateFormatter stringFromDate:[NSDate date]]]; NSMutableString* dateStr=[NSMutableString stringWithString:[adateFormatter stringFromDate:atempdate]];

//int timeDuration = (int)[[moviePlayerController moviePlayer]duration]; NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; NSString *destinationPath = [NSString stringWithFormat:@"%@/%@.mp4",[NSString stringWithFormat:@"%@/Documents/videos",NSHomeDirectory()],dateStr]; [[NSFileManager defaultManager] moveItemAtPath:sourcePath toPath:destinationPath error:nil];

//Playing the video in iOS4 & Previous versions NSString *moviePath = [NSString stringWithFormat:@"%@/%@.mp4",[NSString stringWithFormat:@"%@/Documents/videos",NSHomeDirectory()],videoName];

movieViewController = nil; moviePlayer = nil;

movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:moviePath]]; moviePlayer = [movieViewController moviePlayer]; //[movieViewController.view setFrame:CGRectMake(0.0, 0.0, 320.0, 275.0)]; [moviePlayer setScalingMode:MPMovieScalingModeAspectFill];//

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(MoviePlayerPlaybackDidFinishNotification) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:moviePlayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MoviePlayerPlaybackStateDidChangeNotification) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];

[moviePlayer play];