phonegap的录音插件是Media,用它可以实现录音及播放录音的功能,但是在android与ios系统下的实现方式有些许不同,共有以下几点:
一、创建录音文件
android代码:
//实例化录音类,<span style="font-family: Arial, Helvetica, sans-serif;">window.appRootDir是自定义的文件夹路径</span>
var mediaRec = new Media(window.appRootDir+"test.mp3",
// 录音执行函数
function() {
},
// 录音失败执行函数
function(err) {
}
);
iso代码:
//window.appRootDir是自定义的文件夹路径
window.appRootDir.getFile(
"test.wav",
{create:true,exclusive:false},
function(fileEntry){
//实例化录音类
var mediaRec = new Media("documents://"+window.appRootDir+"test.wav",
// 录音执行函数
function() {
},
// 录音失败执行函数
function(err) {
}
);
},function(){
}
);
以上两段代码首先是创建的文件格式不同,android比较灵活,可以创建任何音频格式的文件,ios经测试MP3好像不支持,wav是可以的。其次就是android在实例化Media对象的时候可以顺便创建test.mp3文件,ios好像必须先创建好test.wav文件,才能实例化Media对象。还有就是new Media的第一个参数,ios系统必须加上"documents://"前缀。
二、解决android创建的录音ios不能播放
android必须创建“.amr”格式的文件,ios才能识别,也就是将以上代码的“.MP3”后缀改为“.amr”后缀,并且将“AudioPlayer.java”中的“startRecording”方法改为以下代码:
public void startRecording(String file) {
switch (this.mode) {
case PLAY:
Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode.");
sendErrorStatus(MEDIA_ERR_ABORTED);
break;
case NONE:
this.audioFile = file;
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP);
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //AMR_NB);
this.recorder.setOutputFile(this.tempFile);
try {
this.recorder.prepare();
this.recorder.start();
this.setState(STATE.MEDIA_RUNNING);
return;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sendErrorStatus(MEDIA_ERR_ABORTED);
break;
case RECORD:
Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
sendErrorStatus(MEDIA_ERR_ABORTED);
}
}