iOS upload voice to server instance

Source: Internet
Author: User

iOS upload voice to server, here is an introduction to use AFN to upload voice files to the service side.

Voice Turn NSData

If it is not nsdata, we should find a way to convert the voice file into NSData before uploading the server. First we get the NSData object for the voice file.


NSData *voicedata = [Message valueforkey:@ "Wavaudiodata"];

Upload Code

The code is as follows Copy Code
Khosturl the main domain name for the app for the developer company, like http://xxx.yyy.cn
Afhttprequestoperationmanager *manager = [[Afhttprequestoperationmanager alloc] Initwithbaseurl:[nsurl URLWithString : Khosturl]];
Manager.requestSerializer.timeoutInterval = 100;

Nsmutabledictionary *paras = [[Nsmutabledictionary alloc]init];
[Paras SetValue:self.msgType forkey:@ "Msgtype"];
In the form of file, the key here is very important "accessory", this is the corresponding to the server, you must not write, be sure to see the interface document, or upload failure
[Paras SetValue: (__bridge ID) ((__bridge FILE *) (VoiceData)) forkey:@ "accessory"];
[Paras Setvalue:[blluser Getpushtoken] forkey:@ "Senderpushtoken"];
[Paras SetValue:self.receiverPushToken forkey:@ "Receiverpushtoken"];
[Paras Setobject:[base getkey] forkey:@ "Ukey"];

afhttprequestoperation * operation = [manager post:kbllurl_chat_chat Parameters:paras constructingbodywithblock:^ (ID <AFMultipartFormData> formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "YYYYMMDDHHMMSS";
NSString *str = [formatter stringfromdate:[nsdate date]];
filename here take the current timestamp
Accessory is still the same as the service side, specifically look at your own interface document
MimeType Choose @ "AMR/MP3/WMR", meaning to tell the server, I uploaded the file type is support three formats AMR, MP3 and WMR, here need to note
Here I uploaded a amr format voice file, so if your voice files for other formats, must be the service end of the corresponding, such as the server to let you upload wav format voice, then you get AMR, how to do? Tell a joke, someone directly change the suffix, the service is not let me upload wav, directly to change. wav not on it! Someone really wants to do that. So here's to tell you, you're a man, you wear a woman's clothes, you're still a man. Even if you cut Tintin is not a woman, at most not male or female. The solution is to find a third-party library, your AMR voice converted to WAV, and then transferred to NSData and then upload it.
NSString *filename = [NSString stringwithformat:@ "%@.amr", str];
[FormData appendpartwithfiledata:voicedata name:@ "accessory" filename:filename mimetype:@ "AMR/MP3/WMR"];
} success:^ (Afhttprequestoperation *operation, NSData * responseobject) {
NSLog (@ "callback succeeded!");

Nserror * ERROR;
Nsdictionary * dic = [nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingallowfragments Error :&error];
JSON resolves to get the dictionary returned by the storage server
NSLog (@ "State ==%@", dic[@ "MSG"]);
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
Failed to connect to server
NSLog (@ "send failed");
}];

Operation.responseserializer = [Afhttpresponseserializer serializer];
[Operation start];


Code can be directly copy, changed BaseURL and path, has been uploaded to key parameters. You can upload it successfully.

Related Article

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.