objective c - AVAudioRecorder prepareToRecord always return NO -


here's code using trigger recording. can suggest solution?

[avaudiorecorder preparetorecord] return no

avaudiosession *audiosession = [avaudiosession sharedinstance]; [audiosession setcategory:avaudiosessioncategoryrecord error:nil];  nsmutabledictionary *recordsettings = [[nsmutabledictionary alloc] initwithcapacity:10]; if(recordencoding == enc_pcm) {     [recordsettings setobject:[nsnumber numberwithint: kaudioformatlinearpcm] forkey: avformatidkey];     [recordsettings setobject:[nsnumber numberwithfloat:44100.0] forkey: avsampleratekey];     [recordsettings setobject:[nsnumber numberwithint:2] forkey:avnumberofchannelskey];     [recordsettings setobject:[nsnumber numberwithint:16] forkey:avlinearpcmbitdepthkey];     [recordsettings setobject:[nsnumber numberwithbool:no] forkey:avlinearpcmisbigendiankey];     [recordsettings setobject:[nsnumber numberwithbool:no] forkey:avlinearpcmisfloatkey]; } else {     nsnumber *formatobject;      switch (recordencoding) {         case (enc_aac):             formatobject = [nsnumber numberwithint: kaudioformatmpeg4aac];             break;         case (enc_alac):             formatobject = [nsnumber numberwithint: kaudioformatapplelossless];             break;         case (enc_ima4):             formatobject = [nsnumber numberwithint: kaudioformatappleima4];             break;         case (enc_ilbc):             formatobject = [nsnumber numberwithint: kaudioformatilbc];             break;         case (enc_ulaw):             formatobject = [nsnumber numberwithint: kaudioformatulaw];             break;         default:             formatobject = [nsnumber numberwithint: kaudioformatappleima4];     }      [recordsettings setobject:formatobject forkey: avformatidkey];     [recordsettings setobject:[nsnumber numberwithfloat:44100.0] forkey: avsampleratekey];     [recordsettings setobject:[nsnumber numberwithint:2] forkey:avnumberofchannelskey];     [recordsettings setobject:[nsnumber numberwithint:12800] forkey:avencoderbitratekey];     [recordsettings setobject:[nsnumber numberwithint:16] forkey:avlinearpcmbitdepthkey];     [recordsettings setobject:[nsnumber numberwithint: avaudioqualityhigh] forkey: avencoderaudioqualitykey]; }  nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/recordtest.caf", [[nsbundle mainbundle] resourcepath]]];  nserror *error = nil; audiorecorder = [[ avaudiorecorder alloc] initwithurl:url settings:recordsettings error:&error];  bool canrecord = [audiorecorder preparetorecord]; if ([audiorecorder preparetorecord] == yes){     [audiorecorder record]; }else {     int errorcode = cfswapint32hosttobig ([error code]);     nslog(@"error: %@ [%4.4s])" , [error localizeddescription], (char*)&errorcode); } 

you need activate avaudiosession:

[audiosession setactive:yes error:&error]; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -