I've scoured the net looking for others with similar problems. I've found similar error messages, but nobody has seen to found any answers. This seems to be a common error message with both the Cordova 2.x series and 3.x series. I get this error when I try to record audio using Cordova's org.apache.cordova.media plugin. Specifically, after creating a media object, running startRecord(), and then when I execute stopRecord(), that is when the error occurs.
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");
}
}
Does anyone have a suggestion on how to fix this?
William - This is an error/bug with the implementation within the plugin. I ran across your question while I was looking for a solution to this for my own project.
The issue lies in the fact that a temp file is created initially to write the audio to, then it is moved and renamed after finishing recording. The File.renameTo() function that is used will not write from internal to SD (or vis versa). I have rewritten the function for my own purposes and it is working great as far as I can see. Below is the updated function.
https://github.com/apache/cordova-plugin-media/blob/master/src/android/AudioPlayer.java
org.apache.cordova.media > AudioPlayer.java Line 32 (add)
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 Line 139 (replace)
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 Line 168 (replace entire function)
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);
}
}
Let me know if this takes care of your issue as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With