我正在开发一个 ionic 应用程序,我需要录制音频文件,所以我使用了 cordova-plugin-media,它在 android 上运行良好,但是当我在 ios 上尝试时,我得到了这个错误:
{"message":"Failed to start recording using AVAudioRecorder","code":1}
这是我的代码:
var extension = '.wav';
var name = 'filename';
var audio = null;
var filepath;
$scope.startRecording = function() {
name = Utils.generateFilename();
if(device.platform == "iOS")
{
//var path = "documents://";
//var path = cordova.file.documentsDirectory;
//var path = cordova.file.cacheDirectory;
var path = cordova.file.dataDirectory;
path = path.replace("file:///", "cdvfile://");
}
else if(device.platform == "Android")
{
var path = cordova.file.externalApplicationStorageDirectory;
}
var filename = name + extension;
filepath = path + filename;
audio = new Media(filepath, function(e){console.log(e, "success Media");},function(e){console.log(e, "error");});
audio.startRecord();
};
$scope.sendAudioMsg = function() {
audio.stopRecord();
audio.release();
};
当我控制台记录路径时,我得到这个:
cdvfile://var/mobile/Containers/Data/Application/F47A76AD-E776-469F-8EFA-85B0A0F14754/Library/NoCloud/dJzSapEU6k16hV7EGrD06L29NjZcOCVzHCUTrcjEDiTCF7uUNGmCHchEcwfGls3V88p85ij0NUkv0KVagevbgh3lwWsEfmAO8uNq.wav
谁能帮帮我??
最佳答案
尝试以下 Android 版。
var path = "Android/data/"+YOUR_APP_ID+"/files/"+FILENAME+".wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();
录制完成后访问文件
var path = cordova.file.externalApplicationStorageDirectory+"files/"+FILENAME+".wav";
var myMedia1 = new Media(path, success, error);
myMedia1.play();
YOUR_APP_ID 将是 com.test.app
iOS,见下文
var fileName = "myrec.wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();
访问文件
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem){
fileSystem.root.getFile(fileName, null, function (fileEntry){
fileEntry.file(function OnFileEntry(file){
// file is actual recorded file, get the path and play or get base64 string
var reader = new FileReader();
reader.onloadend = function(evt) {
evt.target.result; // this is base64 string
};
reader.readAsDataURL(file);
}, error);
}, error);
}, error);
关于ios - ionic Cordova 使用媒体插件在 IOS 上录制音频失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39643801/