离子Cordova失败在IOS使用媒体插件录制音频 [英] ionic Cordova Failed recording audio on IOS using Media plugin

查看:298
本文介绍了离子Cordova失败在IOS使用媒体插件录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作离子应用程序,我需要录制音频文件,所以我使用cordova插件媒体,它工作正常为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/dJzSapEU6k16hV7EGrD06L29NjZcOCVzHCUTrcjEDiTCF7uUNGmCHchEcwfGls3V88p85ij0NUkv0KVagevbgh3lwWsEfmAO8uNq.wav




可以帮助我

解决方案

Android。

  var path =Android / data /+ YOUR_APP_ID +/ files /+ FILENAME +。 

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){
//文件是实际记录的文件,获取路径并播放或获取base64字符串
var reader = new FileReader();
reader.onloadend = function(evt){
evt.target.result; //这是base64字符串
};
reader.readAsDataURL(file);
},错误);
},错误) ;
},error);


i'm working on an ionic App and i need to record audio files , so i used the cordova-plugin-media , it worked fine for android but when i tried it on ios i get this error :

{"message":"Failed to start recording using AVAudioRecorder","code":1}

here is my code :

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();
  };

when i console log the path i get this :

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

can any one help me??

解决方案

Try following for Android.

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

var myMedia = new Media(path, success, error);
myMedia.startRecord();

To access file after you are done with recording

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

YOUR_APP_ID would be com.test.app

iOS, see following

var fileName = "myrec.wav";
var myMedia = new Media(path, success, error);
myMedia.startRecord();

To access file

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);

这篇关于离子Cordova失败在IOS使用媒体插件录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆