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

时间:2016-09-22 16:02:03

标签: ios xcode cordova ionic-framework cordova-plugins

我正在研究离子应用程序,我需要录制音频文件,所以我使用了cordova-plugin-media,它适用于Android但是当我在ios上尝试时我得到了这个错误:

  

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

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

就我而言,我正在使用本机录制GUI

IOS

cordova.file.applicationStorageDirectory + "tmp/OSAudioRecordings/" + file_name;

ANDROID

cordova.file.applicationStorageDirectory + "files/AudioRecords/" + file_name;

答案 1 :(得分: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);

答案 2 :(得分:0)

我的答案仅适用于IOS

尝试使用Media进行录制并将录制文件的位置设置为以下任意组合时,我遇到了同样的错误消息

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

,最终使用Temp目录解析录制文件的位置
    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);
        });