Cordova 3.5.0 - FAILED重命名/storage/emulated/0/tmprecording.3gp

Wil*_*ite 6 android cordova

我在网上搜寻有类似问题的其他人.我发现了类似的错误消息,但没有人发现任何答案.这似乎是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");
    }       
}
Run Code Online (Sandbox Code Playgroud)

有没有人建议如何解决这个问题?

小智 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;
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

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);
}

}
Run Code Online (Sandbox Code Playgroud)

让我知道这是否也能解决您的问题。