ionic4 解决android平台修改采样率、位深度等问题

在研究ionic4 Android平台语音转文字的这功能,接的是科大讯飞的实时语音转写API。
在经过一系列查找文档搜索插件,终于在Cordova官网中搜到了cordova-plugin-audioinput

安装插件如下:

1、cordova plugin add cordova-plugin-audioinput
2、cordova plugin add https://github.com/edimuj/cordova-plugin-audioinput.git

链接地址如下:

1、https://www.npmjs.com/package/cordova-plugin-audioinput
2、https://github.com/edimuj/cordova-plugin-audioinput

简短代码如下:

1.初始化代码、判断是否开启麦克风

var captureCfg = {
  sampleRate: 16000,
  channels: audioinput.CHANNELS.MONO,
  format: audioinput.FORMAT.PCM_16BIT,
  audioSourceType: audioinput.AUDIOSOURCE_TYPE.MIC,
  fileUrl: this.file.externalDataDirectory + 'demo.pcm'
};
// Initialize the audioinput plugin.
window.audioinput.initialize(captureCfg, function () {
  console.log(captureCfg)
  // Now check whether we already have permission to access the microphone.
  window.audioinput.checkMicrophonePermission(function (hasPermission) {
    if (hasPermission) {
      console.log("Already have permission to record.");
    }
    else {
      // Ask the user for permission to access the microphone
      window.audioinput.getMicrophonePermission(function (hasPermission, message) {
        if (hasPermission) {
          console.log("User granted permission to record.");
        } else {
          console.warn("User denied permission to record.");
        }
      });
    }
  });
});
采样率、位深度等设置
  1. 开始录音

    audioinput.start();
    
  2. 停止录音

    audioinput.stop();
    

4.录音文件上传

录音文件上传用的是 cordova-plugin-file-transfer、cordova-plugin-file这两个插件,链接地址分别如下:

 https://github.com/apache/cordova-plugin-file-transfer
 https://github.com/apache/cordova-plugin-file

代码如下:

const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
  fileKey: 'file',
  fileName: src,  //名称
  mimeType: 'audio/wav',
  httpMethod: "POST",
  headers: {
    'Authorization': '......'
  },
  params: {}
}
var api = 'http://XXX....';
fileTransfer.upload(this.file.externalDataDirectory + 'demo.pcm', api, options)
  .then((res: any) => {
    console.log(res)
  }, (err) => {
    console.log(err)
  })

你可能感兴趣的:(ionic4 解决android平台修改采样率、位深度等问题)