ionic Cordova 无法使用媒体插件在 IOS 上录制音频

haf*_*tim 5 xcode ios cordova ionic-framework cordova-plugins

我正在开发一个离子应用程序,我需要录制音频文件,所以我使用了cordova-plugin-media,它在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();
  };
Run Code Online (Sandbox Code Playgroud)

当我控制台记录路径时,我得到了这个:

cdvfile://var/mobile/Containers/Data/Application/F47A76AD-E776-469F-8EFA-85B0A0F14754/Library/NoCloud/dJzSapEU6k16hV7EGrD06L29NjZcOCVzHCUTrcjEDiCHls0jVcjVmGmfchls0jWcjVmGmfchls0jWcjVmGfmfchs0j8vgwmgfchls0jwgwmfgwcf8gwcfwmgwfmgwfcf8gwf14754/library/NoCloud/dJzSapEU6k16hV7

谁能帮我??

use*_*906 0

尝试以下 Android 版。

var path = "Android/data/"+YOUR_APP_ID+"/files/"+FILENAME+".wav";

var myMedia = new Media(path, success, error);
myMedia.startRecord();
Run Code Online (Sandbox Code Playgroud)

完成录制后访问文件

var path = cordova.file.externalApplicationStorageDirectory+"files/"+FILENAME+".wav";
var myMedia1 = new Media(path, success, error);
myMedia1.play();
Run Code Online (Sandbox Code Playgroud)

YOUR_APP_ID 将是 com.test.app

iOS,请参见下文

var fileName = "myrec.wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();
Run Code Online (Sandbox Code Playgroud)

访问文件

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

  • 即使我在 iOS 上使用 .wav,“无法使用 AVAudioRecorder 开始录音”。 (2认同)