Video capturing Issue in ios [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
recored video using UIImagePickerController
I am new in iOS development. Now i am working on video recording app. But my app sometimes record video. And some times it closes camera Give me warning as
UIImagePickerController: ignoring request to stop video capture; camera is not currently capturing video.

I am capturing Video and store in document directory like this way:-
-(IBAction)cameraLibraryButtonClick:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *videoRecorder = [[UIImagePickerController alloc]init];
videoRecorder.delegate = self;
NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:videoRecorder.sourceType];
NSLog(#"Available types for source as camera = %#", sourceTypes);
if (![sourceTypes containsObject:(NSString*)kUTTypeMovie] ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Device Not Supported for video Recording." delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No",nil];
[alert show];
[alert release];
return;
}
videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera;
videoRecorder.mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];
videoRecorder.videoQuality = UIImagePickerControllerQualityTypeLow;
videoRecorder.videoMaximumDuration = 120;
self.imagePicker = videoRecorder;
[videoRecorder release];
[self presentModalViewController:self.imagePicker animated:YES];
newMedia = YES;
}
else {
[self displaysorceError];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
//self.fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
NSString *ZipLibrary = [paths objectAtIndex:0];
NSString *FileFullPath = [ZipLibrary stringByAppendingPathComponent:#"%#.mp4"];
NSLog(#"Ziplinrnr oadfjaidfjidfjidjfid %#",FileFullPath);
[videoData writeToFile:FileFullPath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
-(void)displaysorceError{
UIAlertView *alt = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Camera Image Sorce Not Available"
delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alt show];
[alt release];
}

Related

Overlay on iPhone camera

I want to make an overlay on camera of my iphone app.I have followed this tutorial
But it teaches about capturing the image,I need to implement it on recording the video.
In the tutorial it is simply
- (void)takePicture
{
[picker takePicture];
}
But I am not able to implement it on recording the video,Please help me with an example if you can
Now without overlay I am using this coding for recording the video[I wish to implement it with the above tutorial]
-(void)ViewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onUploadVideoProgress:) name:#"UPLOADPROGRESS" object:nil];
self.mediaTypes=[NSArray arrayWithObjects:(NSString *) kUTTypeMovie,nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
videoRecorder = [[UIImagePickerController alloc] init];
videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera;
videoRecorder.delegate = self;
NSArray *mediaTypes1 = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
NSArray *videoMediaTypesOnly = [mediaTypes1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(SELF contains %#)", #"movie"]];
if ([videoMediaTypesOnly count] == 0)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry but your device does not support video recording"
delegate:nil
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
}
else
{
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
videoRecorder.cameraDevice = UIImagePickerControllerCameraDeviceRear;
videoRecorder.mediaTypes = videoMediaTypesOnly;
videoRecorder.videoQuality = UIImagePickerControllerQualityType640x480;
videoRecorder.videoMaximumDuration = 30.0f;
appDelegate.videoOrientation = #"portrait";
[self presentModalViewController:videoRecorder animated:YES];
}
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry but your device does not support video recording"
delegate:nil
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.videoURL=[[info objectForKey:UIImagePickerControllerMediaURL] path];
self.videoData=[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerMediaURL]];
[self dismissModalViewControllerAnimated:YES];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType compare:(NSString*)kUTTypeMovie] == NSOrderedSame) {
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
moviePlayer.shouldAutoplay=NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
appDelegate.selectedVideo=[NSDictionary dictionaryWithObjectsAndKeys:
self.videoURL,#"videourl",
self.videoData,#"videodata",
thumbnail,#"thumbdata",
nil ];
}
AddOrEditVideoDetails *controller = [[AddOrEditVideoDetails alloc]initWithNibName:#"AddOrEditVideoView"bundle:nil];
[[self navigationController] pushViewController:controller animated:YES];
}
UIImagePickerController already has API for recording video. You can call
– startVideoCapture
– stopVideoCapture
to record video. Check the API

Set maximum recording duration,while video recording from iphone app

In my iPhone app,a user can record the video.I want to set the maximum allowed time of recording is to 30seconds.How to do that,any ideas ?
using this code
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
NSLog(#"movie path %#",moviePath);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
#selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo: (void*)contextInfo {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Video Saving Failed"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Video Saved" message:#"Saved To Photo Album"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
You need to set the videoMaxiumDuration property on UIImagePickerController after configuring it for video recording.
The value is an NSTimeInterval which is specified in seconds, so you'll want to set it to 300 seconds if you want 5 mins of video.

How to save a video using code in objective c?

I have captured a video using objective c,however I am not able to save it in the iphone photos library.I dont want to use the AlAssets library,I want to use the image picker only.I have seen a lot of methods on stack overflow and other sites but they either use storage location path(which is not mentioned what it is) or they dont work.
This is my piece of code.
-(IBAction)Onclick:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)
kUTTypeMovie];
imagePicker.delegate = self;
//UISaveVideoAtPath
imagePicker.allowsImageEditing = NO;
[self.view addSubview:imagePicker.view];
[imagePicker viewWillAppear:YES];
CGRect overlayFrame = CGRectMake(0, 380, 320, 44);
//UILabel *lbl=[[UILabel alloc]init];
UIView *baseView = [[[UIView alloc] init] autorelease];
baseView.backgroundColor =[UIColor greenColor];
//lbl.text=#"dfgfdgfd";
baseView.frame=overlayFrame;
//view.delegate = self;
//view.picker = picker;
//[view customize];
imagePicker.cameraOverlayView = baseView;
[self presentModalViewController:imagePicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil);
}
else if ([mediaType isEqualToString:#"public.movie"]){
NSURL *url = [[[info objectForKey:UIImagePickerControllerMediaURL] copy] autorelease];
// ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
// [library writeVideoAtPathToSavedPhotosAlbum:url
// completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}];
UISaveVideoAtPathToSavedPhotosAlbum(url, nil, nil, nil);
}
[self dismissModalViewControllerAnimated:YES];
}
Try this:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
// Handle a movie capture
if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo)
{
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
{
UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, #selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
}
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
if (error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Photo/Video Saving Failed" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Photo/Video Saved" message:#"Saved To Photo Album" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
}
Hope it helps you.
Try this:
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error){
/*notify of completion*/
NSLog(#"AssetURL: %#",assetURL);
NSLog(#"Error: %#",error);
if (!error) {
//video saved
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:error.domain delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}];

how to prevent saving picture from camera roll again

May be this question is too easy or I did not know how easy it will be.
I have an actionsheet to allow the user to choose how if they want to take a photo or select from camera.
The app will save user taken photo regardless is selected from camera roll or taken from camera. Am I missing something?
Thanks for reading my question.
#pragma mark - photo select
-(IBAction)showCameraAction:(id)sender
{
if(![UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Choose Photo From Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
actionSheet.tag =1;
[actionSheet showInView:[self.view superview]];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo With Camera", #"Choose Photo From Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
actionSheet.tag =1;
[actionSheet showInView:[self.view superview]];
}
}
- (void)getPhotoFromSource:(UIImagePickerControllerSourceType)sourceType;
{
//NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
//NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//picker.mediaTypes = mediaTypes;
picker.delegate = self;
//picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error accessing media" message:#"Device doesn't support media source" delegate:nil cancelButtonTitle:#"Drat" otherButtonTitles:nil, nil];
[alert show];
}
}
#pragma mark UIImagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerOriginalImage];
imageFrame=fImageView.frame;
//UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *shrunkenImage = shrinkImage(orginalImage,imageFrame.size);
self.fImage = shrunkenImage;
fImageView.image = frogImage;
//answer fix, to prevent saving picture from cameraroll again.
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(orginalImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
//NSString *message;
//NSString *title;
// if (!error) {
// title = NSLocalizedString(#"SaveSuccessTitle", #"");
// message = NSLocalizedString(#"SaveSuccessMessage", #"");
// } else {
// title = NSLocalizedString(#"SaveFailedTitle", #"");
// message = [error description];
// }
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
// message:message
// delegate:nil
// cancelButtonTitle:NSLocalizedString(#"ButtonOK", #"")
// otherButtonTitles:nil];
// [alert show];
}
Try this code.. Its working properly from my side.
- (IBAction) uploadPhoto:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Use Photo from Library", #"Take Photo with Camera", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheetAction = ActionSheetToSelectTypeOfSource;
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerControllerSourceType sourceType;
if (buttonIndex == 0) {
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else if(buttonIndex == 1) {
sourceType = UIImagePickerControllerSourceTypeCamera;
}else {
// Cancel
break;
}
if([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
}
picker.allowsImageEditing = NO;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
#pragma mark -
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *imgPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image1.png"]];
//COnvert it to NSData before saving and then save it
NSData *imgData = UIImagePNGRepresentation(image);
[imgData writeToFile:imgPath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
you need to implement UIImagePickerControllerDelegate
Check this function
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{}
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
}
Now use this UIImage
To save this Image You have to specify a path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"photo.png"]];
//COnvert it to NSData before saving and then save it
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];

findig and storing the video file path in iphone

I am trying to find the video file path and get store that path into my sqlite I getting video from the photo lib For that use the code like that
-(IBAction)showVideoLibraryPicker
{
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
picker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeMovie, nil];
[self presentModalViewController:picker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"Error" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
And for taking video i use this code
- (IBAction)showVideoPicker {
//picker = [[UIImagePickerController alloc] init];
//
// picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// picker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
// picker.delegate = self; picker.editing = NO; [self presentModalViewController:picker animated:YES];
//picker = [[[UIImagePickerController alloc] init] autorelease];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(id)kUTTypeMovie];
[self presentModalViewController:picker animated:YES];
//[picker setCameraOverlayView:cameraOverlay];
// picker.navigationBarHidden = YES;
// picker.toolbarHidden = YES;
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
// [self presentModalViewController:picker animated:YES];
}
I am confused how can get the path for storing that video path into sqlite .
I find the photo path but didn't get the video path .
For finding the photo path I use the code like this
NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
NSData *myImage=[[NSData alloc]initWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"%#",documentsDirectory);
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png",camtitle.text]];
[myImage writeToFile:fullPathToFile atomically:YES];
NSLog(#"%#",fullPathToFile);
[myImage release];
How can I do for the video also .I need help to solve it out problem
Thanks in advance
Pl. go thru this doc.
Importantly, Implementing a Delegate for the Camera Interface , Listing 2 in that link.
Read the video file data from the obtained path and store that to documents folder.