iOS XMPP send voice picture solution

Source: Internet
Author: User

iOS XMPP send voice, picture solution, there is a need for friends to refer to below.

currently do IM more is used for XMPP.

Because the project needs to achieve voice and picture delivery.

There are three ways to send voice pictures.

1, XMPPSmack File transfer mode.
2, text stream.
3, Server relay.

Because of the project duration and other reasons, the final choice of the way through the server to achieve these functions, this blog is only used for their own work records, there is nothing wrong place to welcome correct.

Sending a language message needs to be common with Android, and the intended option is to select audio using AMR format. This workload is on the iOS side. So after consultation with Android, choose to use the MP3 format audio encoding.

The first is the realization of the recording function. How to record the recording here will not say. The original format of the recorded audio is the CAF, in order to and Android common here need to transcode the recording.

Come out, code June!!! ~
    Address of the recording file NSString *caffilepath =self.voice.recordpath;            NSString *mp3filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/downloadfile.mp3"];    nsfilemanager* Filemanager=[nsfilemanager Defaultmanager];    if ([FileManager removeitematpath:mp3filepath Error:nil]) {NSLog (@ "delete");                } @try {int read, write;  FILE *PCM = fopen ([Caffilepath cstringusingencoding:1], "RB");                                   Source converted audio File location fseek (PCM, 4*1024, seek_cur);  Skip file header File *mp3 = fopen ([Mp3filepath cstringusingencoding:1], "WB");        MP3 file location generated by output outputs const int pcm_size = 8192;        const int mp3_size = 8192;        short int pcm_buffer[pcm_size*2];                unsigned char mp3_buffer[mp3_size];        lame_t lame = Lame_init ();        Lame_set_in_samplerate (Lame, 11025.0);        LAME_SET_VBR (Lame, vbr_default);                Lame_init_params (lame);     do {       Read = Fread (Pcm_buffer, 2*sizeof (short int), pcm_size, PCM);            if (read = = 0) write = Lame_encode_flush (lame, mp3_buffer, mp3_size);                        else Write = lame_encode_buffer_interleaved (lame, pcm_buffer, read, Mp3_buffer, mp3_size);                    Fwrite (Mp3_buffer, Write, 1, mp3);                } while (read! = 0);        Lame_close (lame);        Fclose (mp3);    Fclose (PCM);    } @catch (NSException *exception) {NSLog (@ "%@", [exception description]);                } @finally the operation required for the audio after the conversion is complete nsdate *dates = [NSDate Date];                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];                [Formatter setdateformat:@ "YYYYMMDDHHMMSS"];                nstimezone* TimeZone = [Nstimezone timezonewithname:@ "asia/beijing"];                [Formatter Settimezone:timezone];        NSString *loctime = [Formatter stringfromdate:dates]; Get the audio Nsdat after transcodingA *data = [NSData Datawithcontentsoffile:mp3filepath];                NSString * FileName = [NSString stringwithformat:@ "%@.mp3", Loctime];                Upload server operation, here is my interface class upload method [Interfaceclassfile upvoice:data topicname:filename];        According to the contract with the server, the address of the file is spelled well. [REC] is an agreement with Android to identify pictures and audio marks NSString * FilePath = [NSString stringwithformat:@ "[rec]220.142.0.120:8080/lovebaby/temp                files/%@/%@ ", Struseri D,filename];                Call XMPP to send an information method, send the address out xmppmessage *message = [xmppmessage messagewithtype:@ "chat" To:self.toJID];                [Message Addbody:filepath];                [[Self appdelegate] xmppstream] sendelement:message]; List data Refresh Nsdictionary * dic = [[Nsdictionary alloc]initwithobjectsandkeys:[nsstring stringwithformat:@ "%@", FilePat                        h], @ "Message", loctime,@ "Messagetime", @ "0", @ "isoutgoing", nil];                [Tabledata Addobject:dic];        [Self.tableview Reloaddata]; [Self.tabLeview scrolltorowatindexpath:[nsindexpath indexpathforrow:[tabledata count]-1 inSection:0] A    Tscrollposition:uitableviewscrollpositionbottom Animated:yes]; }


In this way, a text message is received at the receiving end, which is simply a URL to the resource file.

Do what you need after you get the URL.

The same is true of pictures.

The file upload method is attached below.

#pragma mark-chatvoiceupload+ (NSString *) Upvoice: (NSData *) voiceorpic topicname: (NSString *) picname{NSURL *url = [N        sURL Urlwithstring:ip_uploadvoice];    Create form Requests Asiformdatarequest * request;    request = [Asiformdatarequest Requestwithurl:url];   setting parameters [Request Setpostvalue:struserid forkey:@ "user_id"];        Picture NSString * FileName = picname;        [Request Adddata:voiceorpic Withfilename:filename andcontenttype:@ "Image/jpeg" forkey:@ "Upload_file"];    NSLog (@ "%@", request); Requests [request setdelegate:self];    [Request Setshowaccurateprogress:yes];            [Request startsynchronous];        NSString * str = [[NSString alloc]initwithdata:[request ResponseData] encoding:nsutf8stringencoding];    NSLog (@ "%@", str);    Nserror *error = [request ERROR]; if (error) {//[[[Uialertview alloc]initwithtitle:@] Prompt "message:@" upload error, please retry later "Delegate:self Cancelbutto            ntitle:@ "OK" otherbuttontitles:nil, nil]show];    } return str; }
Reprint please indicate the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.