gpt4 book ai didi

android - Cordova 3.5.0 - 重命名/存储/模拟/0/tmprecording.3gp 失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:36 26 4
gpt4 key购买 nike

我在网上搜索过其他有类似问题的人。我发现了类似的错误消息,但没有人看到找到任何答案。这似乎是 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");
}
}

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

最佳答案

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

}

如果这也能解决您的问题,请告诉我。

关于android - Cordova 3.5.0 - 重命名/存储/模拟/0/tmprecording.3gp 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com