从Cordova Capture获取音频数据的base64 [英] Get base64 of audio data from Cordova Capture

查看:1049
本文介绍了从Cordova Capture获取音频数据的base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ngCordova Capture通过录制音频并在某处(通过REST)发送base64来编写此代码。我可以得到捕获音频工作,但一旦它返回audioURI,我不能从base64文件系统的数据。我的代码如下:

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:

那么如何从.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

推荐答案

有同样的问题,我使用Cordova Capture和Cordova文件插件修复。

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

这篇关于从Cordova Capture获取音频数据的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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