How to save a recorded audio in objective C (iPhone) -
i using av audio framework record audio files on iphone. here current code :
nsarray *dirpaths; nsstring *docsdir; dirpaths = nssearchpathfordirectoriesindomains( nsdocumentdirectory, nsuserdomainmask, yes); docsdir = [dirpaths objectatindex:0]; nsstring *soundfilepath = [docsdir stringbyappendingpathcomponent:@"sound.caf"]; nsurl *soundfileurl = [nsurl fileurlwithpath:soundfilepath]; nsdictionary *recordsettings = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithint:avaudioqualitymin], avencoderaudioqualitykey, [nsnumber numberwithint:16], avencoderbitratekey, [nsnumber numberwithint: 2], avnumberofchannelskey, [nsnumber numberwithfloat:44100.0], avsampleratekey, nil]; nserror *error = nil; audiorecorder = [[avaudiorecorder alloc] initwithurl:soundfileurl settings:recordsettings error:&error]; now have function , want send sound.caf file function like:
nsdata *sounddata = [nsdata datawithcontentsoffile:[[nsbundle mainbundle] pathforresource:soundfilepath oftype:@"caf"]]; but reason above line seems return 0kb file.
any suggestions?
in code, file url not global. try code given
first of have start new audio session:
-(void) initaudiosession { avaudiosession * audiosession = [avaudiosession sharedinstance]; [audiosession setcategory:avaudiosessioncategoryplayandrecord error: &error]; [audiosession setactive:yes error: &error]; } -(void) recordstart { nsarray *dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *docsdir = [dirpaths objectatindex:0]; tempfilepath = [nsurl fileurlwithpath:[docsdir stringbyappendingpathcomponent:[nsstring stringwithformat: @"%@.%@",audiofilename, @"caf"]]]; nsmutabledictionary* recordsetting = [[nsmutabledictionary alloc] init]; [recordsetting setvalue :[nsnumber numberwithint:kaudioformatappleima4] forkey:avformatidkey]; [recordsetting setvalue:[nsnumber numberwithfloat:44100.0] forkey:avsampleratekey]; [recordsetting setvalue:[nsnumber numberwithint: 2] forkey:avnumberofchannelskey]; stringbyappendingpathcomponent: [nsstring stringwithformat: @"%@.%@",audiofilename, @"caf"]]]; recorder = [[ avaudiorecorder alloc] initwithurl:tempfilepath settings:recordsetting error:&error]; [recorder setdelegate:self]; [recorder preparetorecord]; [recorder record]; } -(void) stoprecording{ [recorder stop]; } -(void) playrecord{ avplayer = [[avaudioplayer alloc] initwithcontentsofurl:tempfilepath error:&error]; [avplayer preparetoplay]; [avplayer play]; }
Comments
Post a Comment