cocos creator使用头条小游戏麦克风功能
版本:2.4.2
async Microphone() {
const streamID = await tt.requestMicrophone();
const context = tt.getAudioContext();
const sourceNode = context.createMediaStreamSource(streamID);
// sourceNode.connect(context.destination);
const gainNode = context.createGain();
gainNode.gain.value = 1.5;
sourceNode.connect(gainNode);
gainNode.isSampleSource = true;
const analyser = context.createAnalyser();
sourceNode.connect(analyser);
cc.director.once("closeMicrophone",()=>{
},this)
setInterval(() => {
const array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
let values = 0;
const length = array.length;
// get all the frequency amplitudes
for (let i = 0; i < length; i++) {
values += array[i];
}
const averageVolume = values / length;
console.log(`[API: microphone] average volumn: ${averageVolume}`);
this.label.string = `volumn: ${averageVolume}`
}, 1000);
}