最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get base64 of audio data from Cordova Capture - Stack Overflow

programmeradmin3浏览0评论

I am using ngCordova Capture to write this code by recording audio and send the base64 somewhere (via REST). I could get the Capture Audio to work but once it returns the audioURI, I cannot get the data from the filesystem as base64. My code is below:

$cordovaCapture.captureAudio(options).then(function(audioURI) {
  $scope.post.tracId = $scope.tracId;
  $scope.post.type = 'audio';
  console.log('audioURI:');
  console.log(audioURI);
  var path = audioURI[0].localURL;
  console.log('path:');
  console.log(path);

  window.resolveLocalFileSystemURL(path, function(fileObj) {
    var reader  = new FileReader();
    console.log('fileObj:');
    console.log(fileObj);

    reader.onloadend = function (event) {
      console.log('reader.result:');
      console.log(reader.result);
      console.log('event.result:');
      console.log(event.result);
    }
    reader.onload = function(event2) {
      console.log('event2.result:');
      console.log(event2.target.result);
    };
    reader.readAsDataURL(fileObj);
   console.log(fileObj.filesystem.root.nativeURL + ' ' + fileObj.name);
   $cordovaFile.readAsDataURL(fileObj.filesystem.root.nativeURL, fileObj.name)
   .then(function (success) {
         console.log('success:');
         console.log(success);
         }, function (error) {
         // error
         });
  });

Here is the output in console log:

So how do I get the base64 data from the .wav file?

I have been reading these links:

PhoneGap FileReader/readAsDataURL Not Triggering Callbacks

/

I am using ngCordova Capture to write this code by recording audio and send the base64 somewhere (via REST). I could get the Capture Audio to work but once it returns the audioURI, I cannot get the data from the filesystem as base64. My code is below:

$cordovaCapture.captureAudio(options).then(function(audioURI) {
  $scope.post.tracId = $scope.tracId;
  $scope.post.type = 'audio';
  console.log('audioURI:');
  console.log(audioURI);
  var path = audioURI[0].localURL;
  console.log('path:');
  console.log(path);

  window.resolveLocalFileSystemURL(path, function(fileObj) {
    var reader  = new FileReader();
    console.log('fileObj:');
    console.log(fileObj);

    reader.onloadend = function (event) {
      console.log('reader.result:');
      console.log(reader.result);
      console.log('event.result:');
      console.log(event.result);
    }
    reader.onload = function(event2) {
      console.log('event2.result:');
      console.log(event2.target.result);
    };
    reader.readAsDataURL(fileObj);
   console.log(fileObj.filesystem.root.nativeURL + ' ' + fileObj.name);
   $cordovaFile.readAsDataURL(fileObj.filesystem.root.nativeURL, fileObj.name)
   .then(function (success) {
         console.log('success:');
         console.log(success);
         }, function (error) {
         // error
         });
  });

Here is the output in console log:

So how do I get the base64 data from the .wav file?

I have been reading these links:

PhoneGap FileReader/readAsDataURL Not Triggering Callbacks

https://developer.mozilla/en-US/docs/Web/API/FileReader/readAsDataURL

http://jsfiddle/eliseosoto/JHQnk/

http://munity.phonegap./nitobi/topics/filereader_onload_not_working_with_phonegap_build_2_5_0

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Mar 22, 2015 at 8:19 HP.HP. 19.9k56 gold badges159 silver badges258 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Had same problem, which I fixed using both the Cordova Capture and Cordova File plugin.

navigator.device.capture.captureAudio(function (audioFiles) {
    var audioFile = audioFiles[0],
        fileReader = new FileReader(),
        file;
    fileReader.onload = function (readerEvt) {
        var base64 = readerEvt.target.result;
    };
    //fileReader.reasAsDataURL(audioFile); //This will result in your problem.
    file = new window.File(audioFile.name, audioFile.localURL, 
                           audioFile.type, audioFile.lastModifiedDate, audioFile.size);
    fileReader.readAsDataURL(file); //This will result in the solution.
});
发布评论

评论列表(0)

  1. 暂无评论