Use an open Source library: Opencore-amr for iOS
Https://github.com/feuvan/opencore-amr-iOS
1. After downloading, run the Xcode script,
2. After the script is finished, see the example in the test directory, which is an example of decompression and compression.
I tried it, with AMRN-DEC.C.
+ (Ddmessageentity *) Makemessagefromstream: (Dddatainputstream *) bodydata{int32_t seqno=[Bodydata readInt]; NSString*fromuserid =[Bodydata readUTF]; NSString*touserid =[Bodydata readUTF]; int32_t Msgtime=[Bodydata readInt]; int8_t Msgtype=[Bodydata ReadChar]; //int8_t msgrendertype = [Bodydata ReadChar];Ddmessageentity *msg =[[Ddmessageentity alloc] init]; Msg.msgtype=Msgtype; Msg.msgcontenttype=Msgtype; NSString* Messagecontent =Nil; Nsmutabledictionary* Info =[[Nsmutabledictionary alloc] init]; if(Msgtype = = Ddmessagetypevoice | | msgtype = =Ddgroup_messagetypevoice) { if(Msgtype = =Ddmessagetypevoice) {Msg.msgtype=Message_type_single; Msg.msgcontenttype=Ddmessagetypevoice; }Else{Msg.msgtype=Message_type_temp_group; Msg.msgcontenttype=Ddgroup_messagetypevoice; } int32_t datalength=[Bodydata readInt]; NSData* data =[Bodydata readdatawithlength:datalength]; NSData* VoiceData = [Data Subdatawithrange:nsmakerange (4, [Data length]-4)]; NSString* filename =[NSString stringwithstring:[encapsulator defaultfilename]; //....---------------------*********Nsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*documentsdirectory = [Paths Objectatindex:0]; NSString*voicedirectory = [Documentsdirectory stringbyappendingpathcomponent:@"Voice"]; if( ![[Nsfilemanager Defaultmanager] fileexistsatpath:voicedirectory]) {[[Nsfilemanager Defaultmanager] createdirectoryatpath:voicedirectory withintermediatedirectories:yes attribut Es:nil Error:null]; } nsstring* Fileamrname = [voicedirectory stringbyappendingpathcomponent:[nsstring stringWithFormat:@"%.0f.amr", [[NSDate Date] timeIntervalSince1970]]; NSString* Filewavname = [voicedirectory stringbyappendingpathcomponent:[nsstring stringWithFormat:@"%.0f.wav", [[NSDate Date] timeIntervalSince1970]]; if([VoiceData writetofile:fileamrname Atomically:yes]) {intdecode = [Mkserverhelper amrwbdec:2Sourcefile:fileamrname Andtargetfile:filewavname]; if(Decode = =0) {NSLog (@"format conversion succeeded"); } } Else{NSLog (@"Voice Store Error"); } //....---------------------********* if([VoiceData writetofile:filename Atomically:yes]) {messagecontent=filename; } Else{messagecontent=@"Voice Store Error"; } NSData* Voicelengthdata = [Data Subdatawithrange:nsmakerange (0,4)]; int8_t ch1; [Voicelengthdata getBytes:&ch1 Range:nsmakerange (0,1)]; CH1= CH1 &0x0ff; int8_t CH2; [Voicelengthdata getBytes:&CH2 Range:nsmakerange (1,1)]; CH2= CH2 &0x0ff; int32_t CH3; [Voicelengthdata getBytes:&ch3 Range:nsmakerange (2,1)]; CH3= CH3 &0x0ff; int32_t CH4; [Voicelengthdata getBytes:&CH4 Range:nsmakerange (3,1)]; CH4= CH4 &0x0ff; if((ch1 | ch2 | CH3 | CH4) <0){ @throw[NSException exceptionwithname:@"Exception"Reason@"eofexception"Userinfo:nil]; } intVoicelength = ((Ch1 << -) + (CH2 << -) + (CH3 <<8) + (CH4 <<0)); [Info setobject:@ (voicelength) forkey:voice_length]; [Info setobject:@ (0) forkey:ddvoice_played]; }Else{messagecontent= (NSString *) [Bodydata readUTF]; if([messagecontent Hasprefix:dd_message_image_prefix]) {Msg.msgcontenttype=Ddmessagetypeimage; }} nsstring*attach =[Bodydata readUTF]; Msg.msgid=[Ddmessagemodule Getmessageid]; Msg.seqno=seqno; Msg.msgtime=Msgtime; Msg.touserid=Touserid; Msg.msgcontent=[msg getnewmessagecontentfromcontent:messagecontent]; Msg.attach=attach; if([msg isgroupmessage]) {Msg.sessionid= Touserid;//Group Chat, Touserid represents the session IDMsg.senderid = Fromuserid;//Group chat, Fromuserid indicates the sender I } Else{Msg.sessionid= Fromuserid;//Fromuserid represents the Sender ID as a session ID when a single personMsg.senderid = Fromuserid;//Fromuserid represents the Sender ID when a single person } if([Msg.sessionid IsEqualToString:TheRuntime.userID]) {Msg.sessionid=Touserid; } msg.info=info; returnmsg;}
Helper:
+(int) Amrwbdec:(int) argc sourcefile: (nsstring*) sourcefile andtargetfile: (nsstring*) targetfile{//Char *argv[10]; Const intSizes[] = { A, -, the, -, +, -, -, to,5,6,5,5,0,0,0,0 }; FILE*inch; Charheader[6]; intN; void*wav, *Amr; inch= fopen ([sourcefile utf8string],"RB"); if(!inch) {perror ([sourcefile utf8string]); return 1; } N= Fread (header,1,6,inch); if(n! =6|| MEMCMP (header,"#! amr\n",6) {fprintf (stderr,"Bad header\n"); return 1; } WAV= Wav_write_open ([targetfile utf8string],8000, -,1); if(!wav) {fprintf (stderr,"Unable to open%s\n", [TargetFile utf8string]); return 1; } AMR=Decoder_interface_init (); while(1) {uint8_t buffer[ -], littleendian[ the], *ptr; intsize, I; int16_t outbuffer[ the]; /*Read the mode byte*/N= fread (buffer,1,1,inch); if(N <=0) Break; /*Find the packet size*/size= sizes[(buffer[0] >>3) &0x0f]; N= fread (buffer +1,1, size,inch); if(n! =size) Break; /*Decode the packet*/Decoder_interface_decode (AMR, buffer, Outbuffer,0); /*Convert to Little endian and write to WAV*/ptr=Littleendian; for(i =0; I < the; i++) { *ptr++ = (Outbuffer[i] >>0) &0xFF; *ptr++ = (Outbuffer[i] >>8) &0xFF; } wav_write_data (WAV, Littleendian, the); } fclose (inch); Decoder_interface_exit (AMR); Wav_write_close (WAV); return 0;}
The generated. wav file opens the app with Itools, which you can find in the Voice directory,
IM client iOS will record the Android side of Amr's voice transcoding to WAV format