ios xmpp 发送语音图片解决方案
时间:2014-05-04 11:10:52
收藏:0
阅读:438
ios xmpp 发送语音,图片解决方案,有需要的朋友可以参考下。
目前做IM多是用的xmpp。
因为项目需求需要实现语音和图片的发送。
发送语音图片有三种方法。
1,xmpp smack。文件传输方式。
2,文本流。
3,服务器中转。
因为项目工期等原因,最终选择了通过服务器中转的方式来实现这些功能,本博客只是用于自己工作的记录,有什么不对的地方欢迎指正。
发送语言消息需要和安卓共通,本来预期的方案是选择使用amr格式的音频。这样工作量都压在ios这边。所以和安卓协商后选择使用了mp3格式的音频编码。
首先是录音功能的实现。怎么录音这里就不说了。 录音出来的音频的原始格式是caf,为了和安卓共通这里需要将录音进行转码。
出来吧,代码君!!!~
这样,在接收端接收到的为一条文本信息,里面仅仅只是一个指向资源文件的url地址。
在拿到url后进行自己需要的操作。
图片也是同理。
下面附上文件上传方法。
因为项目需求需要实现语音和图片的发送。
发送语音图片有三种方法。
1,xmpp smack。文件传输方式。
2,文本流。
3,服务器中转。
因为项目工期等原因,最终选择了通过服务器中转的方式来实现这些功能,本博客只是用于自己工作的记录,有什么不对的地方欢迎指正。
发送语言消息需要和安卓共通,本来预期的方案是选择使用amr格式的音频。这样工作量都压在ios这边。所以和安卓协商后选择使用了mp3格式的音频编码。
首先是录音功能的实现。怎么录音这里就不说了。 录音出来的音频的原始格式是caf,为了和安卓共通这里需要将录音进行转码。
出来吧,代码君!!!~
//录音文件的地址
NSString *cafFilePath =self.voice.recordPath;
NSString *mp3FilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/downloadFile.mp3"];
NSFileManager* fileManager=[NSFileManager defaultManager];
if([fileManager removeItemAtPath:mp3FilePath error:nil])
{
NSLog(@"删除");
}
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被转换的音频文件位置
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output 输出生成的Mp3文件位置
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 {
//转换完成后对音频进行需要的操作
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];
//拿到转码后的音频
NSData *data = [NSData dataWithContentsOfFile:mp3FilePath];
NSString * fileName = [NSString stringWithFormat:@"%@.mp3",loctime];
//上传服务器操作,这里是我接口类的上传方法
[InterfaceClassFile upVoice:data toPicName:fileName];
//根据和服务器的约定,拼好文件在服务器的地址。
//[rec]为 和安卓约定用于识别图片和音频的标记
NSString * filePath = [NSString stringWithFormat:@"[rec]220.142.0.120:8080/lovebaby/tempfiles/%@/%@",strUserI
d,fileName];
//调用xmpp发送信息方法,将地址发送出去
XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.toJID];
[message addBody:filePath];
[[[self appDelegate] xmppStream] sendElement:message];
//列表数据刷新
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%@", filePath],
@"Message",loctime,@"MessageTime",@"0",@"isOutGoing",nil];
[tableData addObject:dic];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableData count]-1 inSection:0]
atScrollPosition: UITableViewScrollPositionBottom
animated:YES];
}
这样,在接收端接收到的为一条文本信息,里面仅仅只是一个指向资源文件的url地址。
在拿到url后进行自己需要的操作。
图片也是同理。
下面附上文件上传方法。
#pragma mark - chatVoiceUpload
+(NSString * )upVoice:(NSData *)voiceOrPic toPicName:(NSString *)picName
{
NSURL *url = [NSURL URLWithString:IP_UPLOADVOICE];
//创建表单请求
ASIFormDataRequest * request;
request = [ASIFormDataRequest requestWithURL:url];
//设置参数
[request setPostValue:strUserId forKey:@"user_id"];
//图片
NSString * fileName = picName;
[request addData:voiceOrPic withFileName:fileName andContentType:@"image/jpeg"forKey:@"upload_file"];
NSLog(@"%@",request);
//请求
[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:@"提示" message:@"上传出错,请稍后重试" delegate:self
cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]show];
}
return str;
}
转载请注明出处。
评论(0)