在研究ionic4 Android平臺(tái)語(yǔ)音轉(zhuǎn)文字的這功能,接的是科大訊飛的實(shí)時(shí)語(yǔ)音轉(zhuǎn)寫(xiě)API。
在經(jīng)過(guò)一系列查找文檔搜索插件,終于在Cordova官網(wǎng)中搜到了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
簡(jiǎn)短代碼如下:
1.初始化代碼、判斷是否開(kāi)啟麥克風(fēng)
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.");
}
});
}
});
});

采樣率、位深度等設(shè)置
-
開(kāi)始錄音
audioinput.start(); -
停止錄音
audioinput.stop();
4.錄音文件上傳
錄音文件上傳用的是 cordova-plugin-file-transfer、cordova-plugin-file這兩個(gè)插件,鏈接地址分別如下:
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)
})