【问题标题】:ionic Cordova Failed recording audio on IOS using Media pluginionic Cordova 使用媒体插件在 IOS 上录制音频失败
【发布时间】:2017-01-31 07:29:53
【问题描述】:

我正在开发一个 ionic 应用程序,我需要录制音频文件,所以我使用了 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();
  };

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

cdvfile://var/mobile/Containers/Data/Application/F47A76AD-E776-469F-8EFA-85B0A0F14754/Library/NoCloud/dJzSapEU6k16hV7EGrD06L29NjZcOCVzHCUTrcjEDiTCF7uUNGmCHchEcwfGls3V88p85ij0NUkvfEKVuNq35ij0NUkvfEKVuNq.

谁能帮帮我??

【问题讨论】:

  • 我遇到了同样的问题。你解决了吗?
  • 如果你找到了解决方案请贴出来
  • @Ionut 如果有帮助,请查看我的回答。

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


【解决方案1】:

在 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 上遇到了这个错误,谢谢
  • “使用 AVAudioRecorder 开始录制失败”,即使我在 iOS 上使用 .wav。
【解决方案2】:

我的答案仅适用于 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来使用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);
        });

【讨论】:

  • @SartherisStormhammer 如果有帮助,请查看我的回答。
【解决方案3】:

就我而言,我使用的是原生录制 GUI

IOS

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

安卓

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多