2016-09-22 29 views
2

我正在研究离子应用程序,我需要录制音频文件,因此我使用了cordova-plugin-media,它对Android工作正常,但是当我尝试它时IOS我得到这个错误:离子科尔多瓦使用媒体插件在IOS上录制音频失败

{ “消息”: “无法开始使用AVAudioRecorder记录”, “代码”: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

能任何一个帮助我?

+0

解决使用Temp目录记录的文件的位置我encoutered同样的问题。你解决了这个问题吗? – Ionut

回答

0

请尝试关注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); 
+0

我对android没有任何问题。但我有IOS的这个错误,因为我在标题中提到它,谢谢 –

+0

看到我已经更新了我的答案。 – user5091906

+1

“无法使用AVAudioRecorder开始录制”,即使我在iOS上使用.wav。 – Ionut

0

我的回答是IOS只

试图利用媒体进行记录时和记录文件的位置设置为以下任意组合

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

然后我意识到我有同样的错误信息只传递文件名将文件保存在Temp目录(cordova.file.tempDirectory),所以我最终得到的记录工作在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

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

无法正常工作,与上述答案相同 –

相关问题