
经过一番折腾,终于找到了一种第三方库来转换androID与iOS录音播放格式不兼容的问题。
思路:androID/iOS手机录音传给服务器,参数(录音数据+手机端口类型),然后等到服务器广播数据给玩家,判断端口是androID,将数据保存为为.amr格式,否则保存为.aac格式。不同手机端口调用相应端口函数。
1、设置端口录音格式:
androID–>.amr
public voID record(){ if (isRecording == true ) return; recondpath = Environment.getExternalStorageDirectory().getabsolutePath(); recondpath += "/ione1.amr"; //录音格式 file dirs = new file(recondpath); if (dirs.exists()){ dirs.delete(); } stopRecorder(); isRecording = true; mRecorder = new MediaRecorder(); //设置音源为Micphone mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); //设置封装格式 mRecorder.setoutputFormat(MediaRecorder.OutputFormat.AMR_NB); //设置编码格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder. setAudioEnCodingBitRate(8);//设置音频编码录音比特率 mRecorder.setAudioChannels(1);//设置录制的音频通道数 mRecorder.setAudioSamplingRate(8000); //设置音频采样率记录 mRecorder.setoutputfile(recondpath); try { mRecorder.prepare(); } catch (IOException e) { Log.e(TAG,"prepare() Failed"); } //录音 mRecorder.start(); } public boolean stopRecorder() { if( mRecorder == null ) return false; try{ mRecorder.stop(); mRecorder.reset(); mRecorder.release(); mRecorder = null; }catch ( IllegalStateException e){ e.printstacktrace(); } return false; }
ios–>.aac
-(voID)startAudioRecording{ if(!isRecording) { [self init]; isRecording = YES; NSLog(@"正在录音"); NSMutableDictionary *dicM=[NSMutableDictionary dictionary]; //设置录音格式 [dicM setobject:@(kAudioFormatMPEG4AAC) forKey:AVFormatIDKey];//录音格式 //设置录音采样率,8000是电话采样率,对于一般录音已经够了 [dicM setobject:@(1600) forKey:AVSampleRateKey]; //设置通道,这里采用单声道 [dicM setobject:@(1) forKey:AVNumberOfChannelsKey]; //每个采样点位数,分为8、16、24、32 [dicM setobject:@(8) forKey:AVlinearPCMBitDepthKey]; //录音的质量 [dicM setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey]; //是否使用浮点数采样 [dicM setobject:@(YES) forKey:AVlinearPCMIsfloatKey]; recorder = [[AVAudioRecorder alloc] initWithURL:recordedfile settings:dicM error:nil]; [[AVAudioSession sharedInstance] setcategory:AVAudioSessioncategoryPlayAndRecord error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; [self setSoundSession]; [recorder peakPowerForChannel:0]; [recorder preparetoRecord]; [recorder record]; }}
2、接收服务器传来的需要播放的录音数据,根据不同端口录的音保存为数据相同录音格式:
androID–>.amr
ios–>.aac
//服务器传来录音数据,保存在表voiceTab中 //voiceTab[index] = event.datafunction MainScene:startPlayVoice() if self.isstartRecond == false then -- 没有在录音 if device.platform == "androID" or device.platform == "ios" then --播放录音 local len = #voiceTab if len >= 1 then voicePlay( data ) else print(" 录音播放完了 ") end end endend
function voicePlay(data) if device.platform == "androID" then local function callback(result) -- print("录音播放完成 " ) ReturnRecordingPlayChange(result) end local path = writefileCheckRecond( data.voicebin,data.content ) print("开始播放了 voicePlay ") local args = { 1,path,callback } local sigs = "(ILjava/lang/String;I)I" local luaj = require "cocos.cocos2d.luaj" local classname = "com/cocos2dx/sample/LuaJavaBrIDge" local ok,ret = luaj.callStaticmethod(classname,"sendLuaToJavaAudioRecorPlay",args,sigs) if not ok then print("luaj error:",ret) else print("The ret is:",ret) end elseif device.platform == "ios" then local path = writefileCheckRecond( data.voicebin,data.content ) print("开始播放了 voicePlay ",path) local i = iosAudioStartPlay( path ) endend
function writefileCheckRecond( data,platform ) local path = device.writablePath.."netSprite/" --获取本地存储目录 if not io.exists(path) then lfs.mkdir(path) --目录不存在,创建此目录 end if platform == "androID" then path = path.."recond.amr" elseif platform == "ios" then path = path.."recond.aac" else return; end print("写入完成:"..path) return path;end
3、分析两端口录音互相播放的情况(A端口录音–>B端口播放)
androID–>androID:androID玩家录音为.amr传给服务器,androID玩家接收录音二进制数据保存的也是.amr格式,播放OK。
public voID startPlay(String filePath,int luaFunc){ if( isPlay == true) { Log.d("tag","正在播放中"); return; } try { stopPlay(); isPlay = true; playLuaFun = luaFunc; mPlayer = MediaPlayer.create(Cocos2dxActivity.getInstance(),Uri.parse(filePath)); mPlayer.start();// 开始播放 mPlayer.setonCompletionListener(new MediaPlayer.OnCompletionListener(){ @OverrIDe public voID onCompletion(MediaPlayer m) { Log.d("tag","播放完毕"); LuaJavaBrIDge.callbackLuaFun("1","ReturnRecordingPlayChange",index++); buySuccessJava(1); Log.d(TAG,"playLuaFun = " + playLuaFun); isPlay = false; stopPlay(); } }); }catch(Exception e){ Log.e(TAG,"prepare() Failed"); } }
androID–>iOS:androID玩家录音为.amr传给服务器,iOS玩家接收录音二进制数据保存为.amr格式,调用第三方库将格式.amr转.mav格式就播放OK。
-(voID)playAudio{ if(!isPlay) { if([player isPlaying]) { NSLog(@"停止录音播放"); [player pause]; } else { Nsstring *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"documents"]; audioRecoderSavePath=[Nsstring stringWithFormat:@"%@/%@",documentsDirectory,@"netSprite/"]; recodername= [Nsstring stringWithFormat:@"%@",@"recond.amr"]; tempRecoderPath=[Nsstring stringWithFormat:@"%@%@",audioRecoderSavePath,recodername]; fm = [NSfileManager defaultManager]; //创建文件管理对象 if([fm fileExistsAtPath:tempRecoderPath] == NO)//判断文件是否存在 { NSLog(@"文件不存在"); recodername= [Nsstring stringWithFormat:@"%@",@"recond.aac"]; tempRecoderPath=[Nsstring stringWithFormat:@"%@%@",recodername]; isIos = true; [self startPlayAudio]; return; } else { isIos = false; NSLog(@"文件存在"); } Nsstring *recoderWavname= [Nsstring stringWithFormat:@"%@",@"recond.wav"]; tempWavRecoderPath =[Nsstring stringWithFormat:@"%@%@",recoderWavname]; //第三方库AudioConverter [AudioConverter convertAmrToWavAtPath:tempRecoderPath wavSavePath:tempWavRecoderPath asynchronize:YES completion:^(BOol success,Nsstring * _Nullable resultPath) { if (success) { NSLog(@"amr转wav成功!"); _wavfilePath = resultPath; [self startPlayAudio]; } else { NSLog(@"amr转wav失败!"); } }]; } }}
iOS –>androID:ios玩家录音为.aac格式传给服务器,androID玩家接收录音二进制数据保存为.aac格式,
androID玩家播iOS录的.aac格式OK。
iOS–>iOS:ios玩家录音为.aac格式传给服务器,ios玩家接收录音二进制数据保存为.aac格式,ios玩家播iOS录的.aac格式OK。
参考:
http://www.jb51.cc/article/p-mcdpwusb-bpt.html
http://blog.csdn.net/adalu1986/article/details/50502387
http://www.jianshu.com/p/7dc01b48f8fc
以上是内存溢出为你收集整理的cocos2dx之 android/ios语音交互(二)全部内容,希望文章能够帮你解决cocos2dx之 android/ios语音交互(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)