我在网上搜寻有类似问题的其他人。我发现了类似的错误消息,但没有人发现任何答案。这似乎是Cordova 2.x系列和3.x系列的常见错误消息。当我尝试使用Cordova的org.apache.cordova.media插件录制音频时出现此错误。具体来说,在创建媒体对象后,运行startRecord(),然后执行stopRecord(),即发生错误时。
function recordJournalAudio() {
var mediaRecFile = 'journalEntry-' + app.nextJournalID + '.amr';
if (!app.recording) {
app.mediaRec = new Media(mediaRecFile, function() {
console.log("recordAudio():Audio Success");
},
function(err) {
console.log("recordAudio():Audio Error: "+ err.code);
}
);
$("#recordJournalAudioBtn").button("option", "theme", "b");
// Record audio
app.mediaRec.startRecord();
app.recording = true;
}
if (app.recording) {
app.mediaRec.stopRecord(); //THIS IS WHERE THE ERROR OCCURS
$("#recordJournalAudioBtn").button("option", "theme", "a");
}
}
有没有人建议如何解决这个问题?
答案 0 :(得分:3)
William - 这是插件中实现的错误/错误。在我为自己的项目寻找解决方案时,我遇到了你的问题。
问题在于,最初创建临时文件以将音频写入,然后在完成录制后移动并重命名。使用的File.renameTo()函数不会从内部写入SD(反之亦然)。我已根据自己的目的重写了这个功能,据我所知,它的工作效果很好。以下是更新的功能。
https://github.com/apache/cordova-plugin-media/blob/master/src/android/AudioPlayer.java
org.apache.cordova.media> AudioPlayer.java第32行(添加)
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
org.apache.cordova.media> AudioPlayer.java第139行(替换)
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
this.audioFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + file;
}
else {
this.audioFile = "/data/data/" + handler.cordova.getActivity().getPackageName() + "/cache/" + file;
}
//this.audioFile = file;
org.apache.cordova.media> AudioPlayer.java第168行(替换整个函数)
public void moveFile(String file) {
/* this is a hack to save the file as the specified name */
File newf = new File(file);
String folder = newf.getParent();
if (folder == null) folder = "";
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
String logMsg = "renaming " + this.tempFile + " to " + file;
Log.d(LOG_TAG, logMsg);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(this.tempFile));
} catch (FileNotFoundException e) {
//e.printStackTrace();
Log.e(LOG_TAG, "FAILED to open INPUT stream: " + logMsg);
}
OutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
//e.printStackTrace();
Log.e(LOG_TAG, "FAILED to open OUTPUT stream: " + logMsg);
}
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len; try {
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close();
out.close();
} catch (IOException e) {
//e.printStackTrace();
Log.e(LOG_TAG, "FAILED COPY: " + logMsg);
}
}
如果这也能解决您的问题,请告诉我。