离子Cordova使用Media插件在IOS上录制audio失败

我在离子应用程序上工作,我需要loggingaudio文件,所以我用cordova插件媒体,它工作正常的Android,但是当我尝试了它的ios我得到这个错误:

{“message”:“无法使用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(); }; 

当我控制台loginpath我得到这个:

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

尝试使用“媒体”进行录制并将录制文件的位置设置为以下组合中的任何一种时,我遇到了相同的错误消息

 var pathFile = cordova.file.dataDirectory; pathFile = 'documents://'; pathFile = cordova.file.documentsDirectory; 

然后我意识到,只传递文件名将保存在Temp目录(cordova.file.tempDirectory)的文件,所以我最终得到的logging工作在IOS使用这种方式(我张贴完整的代码,我唯一的说明,我是使用媒体插件压缩,而不是普通的媒体插件)

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

然后我最终通过传递“cordova.file.tempDirectory + src”resolveLocalFileSystemURL使用Temp目录parsinglogging文件的位置

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