问题
i'm working on an ionic App and i need to record audio files , so i used the cordova-plugin-media , it worked fine for android but when i tried it on ios i get this error :
{"message":"Failed to start recording using AVAudioRecorder","code":1}
here is my code :
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();
};
when i console log the path i get this :
cdvfile://var/mobile/Containers/Data/Application/F47A76AD-E776-469F-8EFA-85B0A0F14754/Library/NoCloud/dJzSapEU6k16hV7EGrD06L29NjZcOCVzHCUTrcjEDiTCF7uUNGmCHchEcwfGls3V88p85ij0NUkv0KVagevbgh3lwWsEfmAO8uNq.wav
can any one help me??
回答1:
Try following for Android.
var path = "Android/data/"+YOUR_APP_ID+"/files/"+FILENAME+".wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();
To access file after you are done with recording
var path = cordova.file.externalApplicationStorageDirectory+"files/"+FILENAME+".wav";
var myMedia1 = new Media(path, success, error);
myMedia1.play();
YOUR_APP_ID would be com.test.app
iOS, see following
var fileName = "myrec.wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();
To access file
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);
回答2:
My Answer is for IOS only
I was having the same error message when trying to use Media for recording and setting the location of recorded file to any of below combinations
var pathFile = cordova.file.dataDirectory;
pathFile = 'documents://';
pathFile = cordova.file.documentsDirectory;
Then i realized that passing only file name will save the file in Temp directory (cordova.file.tempDirectory) so i end up getting the recording to work on IOS using this way (I am posting the full code and my only note that i was using the media plugin with compression instead of the normal media plugin)
// Save the recorded fileName
var fileName = Math.floor((Math.random() * 100000) + 1) + ".m4a";
if (ionic.Platform.isIOS()) {
var pathFile = '';
} else {
var pathFile = cordova.file.externalDataDirectory;
}
src = pathFile + fileName;
if (mediaRec == null) {
mediaRec = new Media(src,
// success callback
function () {
console.log("recordCompressedAudio():Audio Success");
},
// error callback
function (err) {
console.log("recordCompressedAudio():Audio Error: " + err.code);
console.log(err);
});
}
// Record MPEG compressed audio, single channel at 16kHz
var options = {
SampleRate: 16000,
NumberOfChannels: 1
}
mediaRec.startRecordWithCompression(options);
console.log("Recording Started");
console.log(mediaRec);
}
Then i end up resolving the location for recorded file using the Temp directory by passing "cordova.file.tempDirectory + src" to resolveLocalFileSystemURL
console.log("Recording Stopped");
console.log(mediaRec);
mediaRec.stopRecord();
mediaRec.release();
resolveLocalFileSystemURL(
cordova.file.tempDirectory + src,
function (entry) {
console.log('cdvfile URI: ');
console.log(entry);
player.src = entry.toURL();
console.log("Saved file location :" + src.toInternalURL());
},
function (error) {
console.log('about to resolve this files errors');
console.log(error);
});
回答3:
In my case, where i was using native recording GUI
IOS
cordova.file.applicationStorageDirectory + "tmp/OSAudioRecordings/" + file_name;
ANDROID
cordova.file.applicationStorageDirectory + "files/AudioRecords/" + file_name;
来源:https://stackoverflow.com/questions/39643801/ionic-cordova-failed-recording-audio-on-ios-using-media-plugin