Sound recording using the media cordova plugin results in an empty file

I use the following code to record audio using the Cordova Media plugin on Android devices. This results in an empty sound file, despite the fact that it successfully returns to stopRecord (). Can someone point out what could be wrong with this code?

$cordovaFile.createFile(cordova.file.dataDirectory, 'new-rec.amr'), false) .then(function (fileObj) { console.log('File created', fileObj); var nativePath = fileObj.nativeURL; resolveLocalFileSystemURL(nativePath, function (entry) { var internalPath = entry.toInternalURL(); var mediaRec = new Media(internalPath, //success callback function (success) { console.log("record success", success); }, //error callback function (err) { console.log("record error: " + JSON.stringify(err)); }); // Start recording audio mediaRec.startRecord(); }); }, function (error) { console.log('Cannot create a file to initiate a new recording', error); }); 
+5
source share
1 answer

I had the same problem today. Solved it by releasing a Media object. I'm not sure why this helped, but I have a feeling that the OS is holding onto the Media object instead of saving it, and then after it released it properly saved?

However, 100%. It works without release in other directories in Cordoba (e.g. cordova.file.externalDataDirectory).

Here is my code:

 var fileName = randomFileName(); fileName = fileName + ".aac"; var store = cordova.file.dataDirectory; window.resolveLocalFileSystemURL(store, function(dir){ var directoryToSave = dir.nativeURL + fileName; $scope.audioRecording = new Media(directoryToSave, audioRecordingSuccess, audioRecordingFail, audioStatus); $scope.audioRecording.startRecord(); setTimeout(function(){ $scope.audioRecording.stopRecord(); $scope.audioRecording.release(); }, 5000); }); 
0
source

Source: https://habr.com/ru/post/1239856/


All Articles