ios - ionic Cordova 使用媒体插件在 IOS 上录制音频失败

标签 ios xcode cordova ionic-framework cordova-plugins

我正在开发一个 ionic 应用程序,我需要录制音频文件,所以我使用了 cordova-plugin-media,它在 android 上运行良好,但是当我在 ios 上尝试时,我得到了这个错误:

{"message":"Failed to start recording using 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();
  };

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

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 - ionic Cordova 使用媒体插件在 IOS 上录制音频失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39643801/

相关文章:

ios - Cordovalib项目未使用Xcodebuild 6.1命令行构建

ios - 由具有 UITableViewAutomaticDimension 的 FetchedResultsController 提供支持的 UITableView - 重新加载表格时单元格移动

objective-c - 如何在没有 facebook Kit 的情况下在 OSX 应用程序中在 facebook 上分享?

SwiftUI Preview - 无法构建方案,没有这样的模块 "ProjectName"

swift - 使用 Firebase 和 Google pod 时发生链接器错误

android - Android Studio 中的 Ionic Cordova(未找到已安装的构建工具)

android - MediaPlayer:在使用Phaser 2.4.6的Cordova 6.1.1中出现错误(1,-2147483648)

ios - 添加约束会使应用程序崩溃

ios - SKProductsRequest 返回 0 个产品

ios - 从扩展上下文中提取 url