npm install pcm-player
import PCMPlayer from 'pcm-player'
this.player.continue();
this.player.pause();
this.player.volume();
this.player.destroy();
createPCMPlayer() {
this.player = new PCMPlayer({
encoding: "16bitInt",
channels: 1,
sampleRate: 16000,
flushingTime: 1000,
onstatechange: (node, event, type) => {},
onended: (node, event) => {},
});
},
initVoiceWebSocket() {
if (typeof WebSocket === "undefined") {
console.log("您的浏览器不支持socket");
} else {
const that = this;
const wsUrl = `ws://192.168.1.1:8080/pcm?audioId=abcd1234`;
this.socket = new WebSocket(wsUrl);
this.socket.onopen = function () {
ws_heartCheck.start();
console.log("指挥室拾音器WebSocket连接已建立");
};
this.socket.onmessage = this.websocketonmessage;
this.socket.onerror = this.websocketonerror;
this.socket.onclose = this.websocketclose;
let ws_heartCheck = {
timeout: 30000,
timeoutObj: null,
serverTimeoutObj: null,
start: function () {
this.timeoutObj = setInterval(function () {
that.socket.send(JSON.stringify({ funId: "voiceHeart", userId: "" }));
}, this.timeout);
},
reset: function () {
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
};
}
}
async websocketonmessage(e) {
const data = await e.data.arrayBuffer();
const audioData = new Uint16Array(data);
this.player.feed(audioData.buffer);
this.player.volume(3);
this.player.continue();
},
websocketonerror(e) {
console.log("WebSocket连接发生错误");
},
websocketclose(e) {
if (this.socket !== null) {
this.socket.close();
}
},