Friday, 15 March 2013

ios - Support for background recording of video using AVCaptureSession -



ios - Support for background recording of video using AVCaptureSession -

i trying record video in background code recording video in foreground when app goes background method.

-(void)captureoutput:(avcapturefileoutput *)captureoutput didfinishrecordingtooutputfileaturl:(nsurl *)outputfileurl fromconnections:(nsarray *)connections error:(nserror *)error

fires error as

error:error domain=avfoundationerrordomain code=-11818 "recording stopped" userinfo=0x176aa180 {nslocalizedrecoverysuggestion=stop other actions using recording device , seek again., nsunderlyingerror=0x1766c0e0 "the operation couldn’t completed. (osstatus error -16133.)", nslocalizeddescription=recording stopped}

i set next value in demoapp-info.plist

required background modes->app plays sound or streams audio/video using airplay application not run in background->no

below code

- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. self.capturesession = [[avcapturesession alloc] init]; //configure video recoring avcapturedevice *videodevice = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; nserror *error = nil; self.videoinput = [avcapturedeviceinput deviceinputwithdevice:videodevice error:&error]; if(self.videoinput){ [self.capturesession addinput:self.videoinput]; } else{ nslog(@"input error:%@", error); } //configure sound recording avcapturedevice *audiodevice = [avcapturedevice defaultdevicewithmediatype:avmediatypeaudio]; avcapturedeviceinput *capturedeviceinput = [avcapturedeviceinput deviceinputwithdevice:audiodevice error:&error]; if(capturedeviceinput){ [self.capturesession addinput:capturedeviceinput]; } //configure display output avcapturevideopreviewlayer *previewlayer = [avcapturevideopreviewlayer layerwithsession:self.capturesession]; [previewlayer setvideogravity:avlayervideogravityresizeaspectfill]; previewlayer.frame = self.view.frame; [self.view.layer addsublayer:previewlayer]; //configure file output //handle event in record start/stop action , in avcapturemoviefileoutput delegate self.capturemoviefileoutput = [[avcapturemoviefileoutput alloc] init]; if([self.capturesession canaddoutput:self.capturemoviefileoutput]){ [self.capturesession addoutput:self.capturemoviefileoutput]; } } - (ibaction)btnrecordclicked:(uibarbuttonitem *)sender { if([[sender title] isequaltostring:@"record"]){ nserror *error = nil; [[uiapplication sharedapplication] beginreceivingremotecontrolevents]; [[avaudiosession sharedinstance] setdelegate:self]; [[avaudiosession sharedinstance] setcategory:avaudiosessioncategoryrecord error:&error]; nslog(@"audiosessionerror:%@", error); [[avaudiosession sharedinstance] setactive:yes error: nil]; self.capturesession.usesapplicationaudiosession = yes; self.capturesession.automaticallyconfiguresapplicationaudiosession = yes; sender.title = @"stop"; [self.capturesession startrunning]; //create file nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"yyyymmddhhmmss"]; nsstring *outputfilename = [[kappdocdirpath stringbyappendingpathcomponent:[dateformatter stringfromdate:[nsdate date]]] stringbyappendingpathextension:@"mp4"]; nsurl *outputfileurl = [[nsurl alloc] initfileurlwithpath:outputfilename]; [self.capturemoviefileoutput startrecordingtooutputfileurl:outputfileurl recordingdelegate:self]; } else{ [[uiapplication sharedapplication] endreceivingremotecontrolevents]; [[avaudiosession sharedinstance] setdelegate:nil]; [[avaudiosession sharedinstance] setactive:no error: nil]; sender.title = @"record"; [self.capturesession stoprunning]; [self.capturemoviefileoutput stoprecording]; } } -(void)captureoutput:(avcapturefileoutput *)captureoutput didfinishrecordingtooutputfileaturl:(nsurl *)outputfileurl fromconnections:(nsarray *)connections error:(nserror *)error { bool issuccess = yes; nslog(@"error:%@", error); if(error.code != noerr) { id value = [[error userinfo] objectforkey:averrorrecordingsuccessfullyfinishedkey]; if(value) { issuccess = [value boolvalue]; } } if(issuccess) { alassetslibrary *assetslibrary = [[alassetslibrary alloc] init]; if([assetslibrary videoatpathiscompatiblewithsavedphotosalbum:outputfileurl]) { [assetslibrary writevideoatpathtosavedphotosalbum:outputfileurl completionblock:^(nsurl *asseturl, nserror *error) { if(error){ nslog(@"error:%@", error); } //delete temporary file nserror *aerror = nil; [[nsfilemanager defaultmanager] removeitematurl:outputfileurl error:&aerror]; }]; } else{ nslog(@"could not saved photos album."); } } }

ios objective-c avfoundation avcapturesession

No comments:

Post a Comment