使用cordova媒体插件录制音频会导致空文件

gop*_*uri 6 audio android cordova

我正在使用以下代码在Android设备上使用Cordova Media插件录制音频.尽管在stopRecord()上成功返回,但这会导致空音频文件.有人可以指出这段代码可能有什么问题吗?

$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);
    });
Run Code Online (Sandbox Code Playgroud)

小智 1

我今天遇到了完全相同的问题。通过释放 Media 对象解决了这个问题。我不确定为什么这有帮助,但我有一种感觉,操作系统可能会保留媒体对象而不是保存它,然后一旦释放它就会正确保存?

但不是 100% 确定。它无需在其他 Cordova 目录(例如 cordova.file.externalDataDirectory)中发布即可工作。

这是我的代码:

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

});
Run Code Online (Sandbox Code Playgroud)