Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - Phonegap / Cordova - read file as text from temporary directory

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...