ios xmpp 发送语音,图片解决方案,有需要的朋友可以参考下。
//录音文件的地址 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]; }
#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; }转载请注明出处。
ios xmpp 发送语音图片解决方案,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/yulang314/p/3705611.html