IOS-Voice Cloud communications

Source: Internet
Author: User
Tags base64 encode

IOS SDK 2.0 voice and picture messages
This document will detail the voice and image messaging interface functions and instructions for the cloud. Before reading this article, let's assume that you have read the cloud-based IOS development guide and have mastered the basic usage of the cloud SDK.

Voice message
Used to send voice fragment messages, you can send voice messages through the Cloud client Imlib interface or the Server API interface. If you are using a cloud-imkit, the feature is already packaged in the SDK and can be used directly. The following is a way to send voice messages through the cloud Imlib and the Server API.

Send a message from a client
Gets the voice data to be sent Wavdata, and obtains the voice time, through the SendMessage method, passes the corresponding parameter to send the voice message, the method explanation see the API document link.

Message Class Name: Rcvoicemessage

code example:

-(void) Sendvoicemessage: (NSData *) wavdata Duration: (long) Duration Targetid: (NSString *) Targetid Conversationtype: ( Rcconversationtype) Conversationtype {
Voice message Entity
Rcvoicemessage *voicemessage = [Rcvoicemessage messagewithaudio:wavdata duration:duration];

[[Rcimclient sharedrcimclient] Sendmessage:conversationtype targetid:targetid content:voicemessage pushContent:nil Pushdata:nil success:^ (Long messageId) {
NSLog (@ "Send voice message success");
} error:^ (Rcerrorcode nerrorcode, long messageId) {
NSLog (@ "Send voice message failed, error code is (%LD)", (long) nerrorcode);
}];
}
The recommended recording parameters are as follows:

Rcvoicerecorderhandler.recordsettings = @{
Avformatidkey: @ (KAUDIOFORMATLINEARPCM),
Avsampleratekey: @8000.00f,
Avnumberofchannelskey: @1,
Avlinearpcmbitdepthkey: @16,
Avlinearpcmisnoninterleaved: @NO,
Avlinearpcmisfloatkey: @NO,
Avlinearpcmisbigendiankey: @NO
};
Note: It is recommended that the voice message be at least 1 seconds long and not longer than 60 seconds. The Voice format is WAV, in the process of message communication, the voice message is converted into AMR format and transmitted after BASE64 encoding.
Sending messages from the server
If you need to send a voice message from the server, the cloud also provides the servers API interface, as an example of sending a single-chat message, which shows how to send a voice message.

Before calling the Server API interface to send a voice message, you need to Base64 encode the speech content Encode, you need to replace all \ r \ n to empty, otherwise it will not be resolved correctly.

Message OBJECTNAME:RC:VCMSG

Structure of the message: {"Content": "bhzpzjximrwrtvc=", "duration": 7, "Extra": ""}

wherein the content for the voice message recording transcoding into AMR format, after the BASE64 encoded Encode result value, duration for the length of the voice message (in seconds), extra can be placed arbitrary data content, you can also remove this attribute.

Note: After the content is BASE64 encoded Encode, you need to replace all \ r \ n into empty, otherwise the correct parsing cannot be done. It is recommended that the voice message be at least 1 seconds long and not longer than 60 seconds. The Voice format is AMR.
code example:

Request:

Post/message/private/publish.json http/1.1
Host:api.cn.ronghub.com
App-key:uwd1c0sxdlx2
timestamp:1408706337
nonce:14314
Signature:890b422b75c1c5cb706e4f7921df1d94e69c17f4
content-type:application/x-www-form-urlencoded

content={"Content": "Iyfbtvikpjexfr5meehgaeev8", "duration": 3, "Extra": "Helloextra"}&fromuserid=2191& Touserid=2191&touserid=2192&objectname=rc:txtmsg&pushcontent=thisisapush&pushdata={\ "PushData\ ": \" hello\ "}&count=1
Response:

http/1.1 OK
Content-type:application/json; Charset=utf-8

{"Code": 200}
To send a single-chat message method parameter description, see the documentation.

Receive voice message parsing
When a voice message is received by the cloud SDK, it is parsed in the following way:

-(void) Decodewithdata: (NSData *) Data {
#if 1
__autoreleasing nserror *__error = nil;
if (!data) {
Return
}
Nsdictionary *dictionary = [Nsjsonserialization jsonobjectwithdata:data
Options:kniloptions
error:&__error];
Rcdictionary *json = [[Rcdictionary alloc] initwithdictionary:dictionary];
#else
NSString *jsonstream =
[[NSString Alloc] Initwithdata:data encoding:nsutf8stringencoding];
Nsdictionary *json = [Rcjsonconverter dictionarywithjsonstring:jsonstream];
#endif
if (JSON) {
NSString *base64audio = [json stringobjectforkey:@ "content"];

Self.extra = [json stringobjectforkey:@ "Extra"];

if (Base64audio) {
NSData *audiodata = nil;
if (Class_getinstancemethod (
[NSData class],
@selector (initwithbase64encodedstring:options:))) {
Audiodata = [[NSData alloc] Initwithbase64encodedstring:base64audio
Options:kniloptions];
} else {
Audiodata = [Rcutilities Datawithbase64encodedstring:base64audio];
}

NSData *decodeddata = [[Rcamrdataconverter sharedamrdataconverter]
Dcodeamrtowave:audiodata];
Self.wavaudiodata = Decodeddata;
}

Self.duration = [[JSON numberobjectforkey:@ "duration"] longvalue];

}
}
Picture message
Used to send picture messages, you can send a picture message via the Cloud client Imlib interface or the Server API interface. If you are using a cloud-imkit, the feature is already packaged in the SDK and can be used directly. Here's how to send picture messages via cloud Imlib and the Server API.

Picture sending mechanism
Picture messages include two main parts: thumbnails and large images, thumbnails directly Base64 encoded into the content, the big picture first uploaded to the file server (the cloud SDK by default upload to seven Cow cloud storage), and then the cloud storage on the large map address into the message body. The flow diagram is as follows:

If you send a picture message, you want to upload the picture to your own server and send a picture message. The flow diagram is as follows:

Thumbnail image mechanism
The thumbnail size is: x 240 pixels, in width and height of the longer edge of not more than 240 pixels, etc. than compression.

Large image size: 960 x 960 pixels, in width and height of the longer edge of not more than 960 pixels, etc. than compression.

Send a message from a client
Call the SendMessage method and pass in the corresponding parameter to send the picture message. For a description of the method, see the API documentation link.

Message Class Name: Rcimagemessage

code example:

-(void) Sendimagemessage: (UIImage *) originimage Targetid: (NSString *) Targetid Conversationtype: (Rcconversationtype) Conversationtype {
Picture message Entity
Rcimagemessage *imagemessage = [Rcimagemessage messagewithimage:originimage];

Set full to Yes to send the original image. Does not set the default send compressed picture.
Imagemessage.full = YES;

[[Rcimclient sharedrcimclient] Sendimagemessage:conversationtype targetid:targetid content:imageMessage pushContent : nil progress:^ (int progress, long messageId) {
Update File Upload progress here
} success:^ (Long messageId) {
Upload successful
} error:^ (Rcerrorcode errorCode, long messageId) {
Upload failed
}];
}
If, when you send a picture message, you want to upload the picture to your own server and send a picture message, the code example is as follows:

-(void) Sendimagemessage2appserver: (UIImage *) originimage Targetid: (NSString *) Target Conversationtype: ( Rcconversationtype) Conversationtype {
Picture message Entity
Rcimagemessage *imagemessage = [Rcimagemessage messagewithimage:originimage];

[[Rcimclient sharedrcimclient] Sendimagemessage:conversationtype targetid:target content:imagemessage pushContent: Nil Pushdata:nil uploadprepare:^ (Rcuploadimagestatuslistener *uploadlistener) {
Upload it here. Upload to tell cloud status and results
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
int i = 0;
for (i = 0; i <; i++) {
Upload images to your server, and notify the cloud progress, progress range 0-100
Uploadlistener.updateblock (i);
[Nsthread sleepfortimeinterval:0.2];
}
Notification of cloud upload results, parameter is the URL of the successful upload
Uploadlistener.successblock (@ "http://www.rongcloud.cn/images/newVersion/bannerInner.png?0717");
});
} progress:^ (int progress, long messageId) {
Update the UI progress here.
} success:^ (Long messageId) {
Message sent successfully
} error:^ (Rcerrorcode errorCode, long messageId) {
Message failed to send
}];
}
For a description of the Sendimagemessage method, see the API documentation link.

Sending messages from the server
If you need to send a picture message from the server, the cloud also provides the servers API interface, as an example of sending a single chat message below, explaining how to send a picture message.

Message OBJECTNAME:RC:IMGMSG

Structure of the message: {"Content": "bhzpzjximrwrtvc=", "Imageuri": "Http://p1.cdn.com/fds78ruhi.jpg", "Extra": ""}

The content for the picture contents for the BASE64 encoded result value, Imageuri for the image uploaded to the image storage server after the address, extra can put arbitrary data content, you can also remove this attribute.

code example:

Request:

Post/message/private/publish.json http/1.1
Host:api.cn.ronghub.com
App-key:uwd1c0sxdlx2
timestamp:1408706337
nonce:14314
Signature:890b422b75c1c5cb706e4f7921df1d94e69c17f4
content-type:application/x-www-form-urlencoded

content={"Content": "bhzpzjximrwrtvc=", "icon": "Http://www.demo.com/p1.png", "Imageuri": "HTTP://WWW.DEMO.COM/1." JPG "," Extra ":" Helloextra "}&fromuserid=2191&touserid=2191&touserid=2192&objectname=rc:txtmsg &pushcontent=thisisapush&pushdata={\ "pushdata\": \ "hello\"}&count=1
Response:

http/1.1 OK
Content-type:application/json; Charset=utf-8

{"Code": 200}
To send a single-chat message method parameter description, see the documentation.

IOS-Voice Cloud communications

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.