PhoneGap的/科尔多瓦 - 读文件从临时目录中的文本 [英] Phonegap / Cordova - read file as text from temporary directory

查看:171
本文介绍了PhoneGap的/科尔多瓦 - 读文件从临时目录中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是科尔多瓦媒体插件录制.wav音频样本。

其实我能记录他们,但我不能找到让已创建的文件内容的方式。

如果我做的:

  //录制音频
//
功能recordAudio(){
  变种SRC =myrecording.wav;
  VAR mediaRec =新媒体(SRC,的onSuccess,onError的);    //录制音频
    mediaRec.startRecord();    // 10秒后停止录制
    变种RECTIME = 0;
    VAR recInterval =的setInterval(函数(){      RECTIME = RECTIME + 1;
      setAudioPosition(RECTIME +秒);      如果(RECTIME> = 3){
        clearInterval(recInterval);        mediaRec.sto precord();
         window.requestFileSystem(LocalFileSystem.TEMPORARY,0,功能(FS){
          fs.root.getFile(myrecording.wav,{创建:真实,独家:假},
            功能(进入){
              的console.log(控制台洛文件);
              的console.log(输入);
        },函数(){
          警报(文件创建错误);
        });
        }, 空值);
      }
    },1000);
  }

我得到在控制台的条目是:

<$p$p><$c$c>{\"isFile\":true,\"isDirectory\":false,\"name\":\"myrecording.wav\",\"fullPath\":\"/myrecording.wav\",\"filesystem\":\"<FileSystem: temporary>\",\"nativeURL\":\"file:///Users/iamuser/Library/Developer/CoreSimulator/Devices/FC61A8C0-CF4B-4A1C-B22C-D8511B1B1707/data/Containers/Data/Application/716F442C-5076-4C69-9CA9-A1A8C2827898/tmp/myrecording.wav\"}

但这不是文件内容,如何检索文件从文件的文本://目录

该文件使用媒体插件到文档记录://audio.wav路径,这似乎是一个临时文件


解决方案

媒体插件是由它的描述意味着


  

提供录制和播放音频文件的能力。


所以,我想这是不会给你的原始数据。然而,当你知道你的音频文件的URI,使用文件的插件从中获取数据。

更新

如果您要上传的文件,请参阅<一个href=\"http://stackoverflow.com/questions/10770122/phonegap-how-can-i-upload-an-audio-file-after-recording-with-phonegaps-media?rq=1\">here.

如果你不是希望得到您的音频文件为Base64 EN codeD字符串,请参考这里

这是一个相当长的code,并在有AngularJS,所以我把主要部分作为一个总结在这里等你。

 函数OnFileEntry(文件){
  VAR读卡器=新的FileReader();
  reader.onloadend =功能(EVT){
      VAR图像= evt.target.result;
      VAR base64Data = image.replace(/ ^数据:音频\\ / MPEG; BASE64,/,);
      base64Data + = base64Data.replace('+','');
  };
  reader.readAsDataURL(文件);
}功能OnGetFile(FileEntry的){
  fileEntry.file(OnFileEntry,失败);
}功能OnFileSystem(文件系统){
  fileSystem.root.getFile(recordName,空,OnGetFile,失败);
}window.requestFileSystem(LocalFileSystem.PERSISTENT,0,OnFileSystem,失败);

你需要改变这里是什么recordName来匹配你的src属性的值。

是够你,还是有别的东西,我可以帮忙的吗?

i am using the cordova media plugin to record .wav audio samples.

Actually i am able to record them but i can't find the way to get the already created file content.

If i do:

// Record audio
//
function recordAudio() {
  var src = "myrecording.wav";
  var mediaRec = new Media(src, onSuccess, onError);

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {

      recTime = recTime + 1;
      setAudioPosition(recTime + " sec");

      if (recTime >= 3) {
        clearInterval(recInterval);

        mediaRec.stopRecord();
         window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs){
          fs.root.getFile("myrecording.wav", {create: true, exclusive: false},
            function(entry){
              console.log("console loggin file");
              console.log(entry);
        }, function(){
          alert("file create error");
        });
        }, null);
      }
    }, 1000);
  }

i get entry this in console:

{"isFile":true,"isDirectory":false,"name":"myrecording.wav","fullPath":"/myrecording.wav","filesystem":"<FileSystem: temporary>","nativeURL":"file:///Users/iamuser/Library/Developer/CoreSimulator/Devices/FC61A8C0-CF4B-4A1C-B22C-D8511B1B1707/data/Containers/Data/Application/716F442C-5076-4C69-9CA9-A1A8C2827898/tmp/myrecording.wav"}

but this is not the file content, how to retrieve the file as text from documents:// directory?

The file is recorded using the media plugin into documents://audio.wav path which seems to be a temporary file

解决方案

Media plugin is by it's description meant to

provide the ability to record and play back audio files

So I suppose it isn't going to give you the raw data. However, as you know the URI of your audio file, use the File plugin to fetch the data from it.

Update

If you want to upload the file, refer to here.

If you instead want to get your audio file as Base64 encoded String, refer to here.

That is quite a long code and has AngularJS in it, so I took the essential parts as a summary here for you

function OnFileEntry(file){
  var reader = new FileReader();
  reader.onloadend = function(evt) {
      var image = evt.target.result;
      var base64Data  =   image.replace(/^data:audio\/mpeg;base64,/, "");
      base64Data  +=  base64Data.replace('+', ' ');
  };
  reader.readAsDataURL(file);
}

function OnGetFile(fileEntry){
  fileEntry.file(OnFileEntry, fail);
}

function OnFileSystem(fileSystem){
  fileSystem.root.getFile(recordName, null, OnGetFile, fail);
}

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, OnFileSystem, fail);

What you need to change here is the recordName to match your src attribute's value.

Is that enough for you, or is there something else I can help you with?

这篇关于PhoneGap的/科尔多瓦 - 读文件从临时目录中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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