gpt4 book ai didi

javascript - Phonegap - 如何在使用 Phonegap 的 media.startRecord 录制后上传音频文件

转载 作者:行者123 更新时间:2023-11-29 10:21:33 25 4
gpt4 key购买 nike

我用 Phonegap 完成了标准录音:

function recordSound() {

var src = "mysound.mp3";
var mediaRec = new Media(src, onSuccess, onError);

// Record audio
mediaRec.startRecord();

// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition(recTime + " sec");
if (recTime >= 10) {
clearInterval(recInterval);
mediaRec.stopRecord();
}
}, 1000);

}

我现在想上传这个文件 (mysound.mp3),而不是让用户自己选择。任何帮助将不胜感激。

到目前为止我已经做了:

function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}

function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}

function up() {// !! Assumes variable fileURI contains a valid URI to a text
// file on the device

var fileURI = "/mnt/sdcard/mysound.mp3";
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);
options.mimeType = "audio/mp3";

var params = new Object();
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(fileURI, "http://myserver/upload.php", win, fail, options);
}

我得到:java.io.IOException:从服务器接收

JSCallback 错误:请求失败

很高兴得到帮助。

最佳答案

您可以使用此功能上传文件

function uploadVoice(fileName, dirName, fileMime, uploadURL) {

var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
};

// upload failure
var fail = function(error) {
alert("An error has occurred: Code = " + error.code);
};

// file system fail
var fsFail = function(error) {
alert("failed with error code: " + error.code);

};

var dirFail = function(error) {
alert("Directory error code: " + error.code);

};

var fileURI;

var gotFileSystem = function (fileSystem) {
fileSystem.root.getDirectory(dirName, {
create: false
}, function (dataDir) {

fileURI = dataDir.fullPath;
fileURI = fileURI + '/' + fileName;

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = fileMime;

var params = new Object();
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(fileURI, uploadURL, win, fail, options);

}, dirFail);

};

// get file system to copy or move image file to
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);

}

希望对你有帮助

关于javascript - Phonegap - 如何在使用 Phonegap 的 media.startRecord 录制后上传音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770122/

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