从 Cordova Capture 中获取音频数据的 base64

5

我正在使用ngCordova Capture记录音频并通过REST发送Base64代码。虽然我能够使“Capture Audio”正常工作,但是一旦它返回音频URI,我就无法从文件系统中获取Base64数据。以下是我的代码:

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

以下是控制台日志输出:

enter image description here

那么我该如何从.wav文件中获取base64数据呢?

我一直在阅读这些链接:

PhoneGap FileReader/readAsDataURL未触发回调

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

http://jsfiddle.net/eliseosoto/JHQnk/

http://community.phonegap.com/nitobi/topics/filereader_onload_not_working_with_phonegap_build_2_5_0

1个回答

7

我遇到了同样的问题,使用Cordova Capture和Cordova File插件解决了这个问题。

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

有没有一种方法可以用视频来完成这个任务? - stackexchange12
@stackexchange12 是的,我也为视频做了这件事。请使用capture.captureVideo。 - Niels Steenbeek
你能看一下我的问题@NielsSteenbeek吗?不幸的是,我无法让您的解决方案起作用。非常感谢。https://stackoverflow.com/questions/51254272/readasdataurl-does-not-process-file-from-captureaudio - Bart S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接