Cordova / PhoneGap,使用audioinput插件录制音频文件直到静音

问题描述 投票:1回答:1

我需要将使用cordova-plugin-audioinput录制的音频文件上传到Web服务。我需要使用这个插件而不是核心插件,因为audioinput支持wav音频格式,这是Web服务唯一接受的格式。

检测到静音时,必须触发“停止录制”,然后“上传”动作(这是我需要帮助的任务)。

我想我必须以某种方式处理波形以检测静音,因此要学习如何在录制时实时读取音频数据我正在尝试实现以下示例:

https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/fftSize

这是我的代码:

audioinput.start({
    sampleRate: 24000,
    fileUrl: cordova.file.cacheDirectory + 'test.wav',
    streamToWebAudio: true
});

const audioCtx = audioinput.getAudioContext();
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 2048;
var bufferLength = analyser.fftSize;
var dataArray = new Uint8Array(bufferLength);

let dataArray;

function draw () {
    requestAnimationFrame(draw);
    dataArray = new Uint8Array(bufferLength);
    analyser.getByteTimeDomainData(dataArray);
    console.log(dataArray);
    // ... draw the waveform ...
    // ... or detect silence ...
}

draw();

遗憾的是,结果是在draw()函数中,所有audioData的项都有128作为值。

我正在测试设备(OnePlus 6与Android 9,但这也适用于iOS)。

谢谢!

cordova phonegap web-audio web-audio-api
1个回答
0
投票

你没有在你的片段中显示它,但是你忘了把某些东西连接到AnalyserNode的输入?如果没有连接,节点的输入为零,getByteTimeDomainData将为这些返回128(由https://webaudio.github.io/web-audio-api/#dom-analysernode-getbytetimedomaindata给出)

© www.soinside.com 2019 - 2024. All rights reserved.