Commit 6cf9c4e8 by 李宁

1

1 parent cd04d846
...@@ -78,17 +78,7 @@ ...@@ -78,17 +78,7 @@
</div> </div>
</div> </div>
<div class="swDown" id="tipDiv" style="display: flex;"> <div class="swDown" id="tipDiv">
<div class="box">
<label>麦克风</label>
<select id="micSelect"></select>
</div>
<div class="box">
<label>扬声器</label>
<select id="spkSelect"></select>
</div>
<div class="buttDiv" style="flex-grow: 1;"> <div class="buttDiv" style="flex-grow: 1;">
<div class="one none clickButt" id="readButt" key="ok">准备好了</div> <div class="one none clickButt" id="readButt" key="ok">准备好了</div>
......
...@@ -1035,15 +1035,7 @@ async function init(){ ...@@ -1035,15 +1035,7 @@ async function init(){
await this.room.localParticipant.setCameraEnabled(true) await this.room.localParticipant.setCameraEnabled(true)
util.toast('摄像头开启成功') util.toast('摄像头开启成功')
await this.room.localParticipant.setMicrophoneEnabled(true,{ await this.room.localParticipant.setMicrophoneEnabled(true)
echoCancellation : true,
noiseSuppression : true,
autoGainControl : true,
// 关键:把“播放”通道排除掉,避免浏览器混音
channelCount : 1,
sampleRate : 48000,
})
util.toast('麦克风开启成功') util.toast('麦克风开启成功')
// await room.localParticipant.enableCameraAndMicrophone() // await room.localParticipant.enableCameraAndMicrophone()
...@@ -1082,101 +1074,6 @@ async function init(){ ...@@ -1082,101 +1074,6 @@ async function init(){
},2000) },2000)
} }
//华为杂音处理代码
/* ========== 工具函数 ========== */
// 获取全部音频设备
async function getAudioDevices(){
// 必须提前拿到一次用户媒体,否则 label 为空
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices){
alert('浏览器不支持 enumerateDevices');
return;
}
await navigator.mediaDevices.getUserMedia({audio:true});
const devices = await navigator.mediaDevices.enumerateDevices();
devices.forEach((item,index)=>{
if(!item.label){
item.label = item.kind + index
}
})
console.log('麦克风和音频轨道输出')
console.log(devices)
console.log('麦克风和音频轨道输出')
return {
mics: devices.filter(d=>d.kind==='audioinput'),
spks: devices.filter(d=>d.kind==='audiooutput')
};
}
// 切换麦克风
async function changeMic(deviceId){
// 先停掉旧轨道
if (window.currentMicStream){
window.currentMicStream.getTracks().forEach(t=>t.stop());
}
// 新轨道
const stream = await navigator.mediaDevices.getUserMedia({
audio:{deviceId:deviceId?{exact:deviceId}:undefined}
});
window.currentMicStream = stream;
console.log('已切换到麦克风:',deviceId||'default');
}
// 切换扬声器(WebAudio)
async function changeSpk(deviceId){
if (!window.audioCtx){
window.audioCtx = new (window.AudioContext||window.webkitAudioContext)();
}
const ctx = window.audioCtx;
// Chrome/Edge 110+ 才支持 setSinkId
if (typeof ctx.setSinkId === 'function'){
await ctx.setSinkId(deviceId);
console.log('已切换到扬声器:',deviceId||'default');
}else{
console.warn('浏览器不支持 WebAudio setSinkId,切换未生效');
}
}
/* ========== UI 渲染 ========== */
function renderSelect($select, devices, type){
$select.empty();
devices.forEach(d=>{
$select.append(`<option value="${d.deviceId}">${d.label||'未知设备'}</option>`);
});
// 默认选中第一项(系统默认)
$select.prop('selectedIndex',0);
}
function playPrompt(buffer) {
const offCtx = new OfflineAudioContext(1, buffer.sampleRate * 0.02, buffer.sampleRate);
const src = offCtx.createBufferSource();
src.buffer = buffer;
src.connect(offCtx.destination);
src.start();
offCtx.startRendering().then(() => {
// 真正播放
const src2 = audioCtx.createBufferSource();
src2.buffer = buffer;
src2.connect(gainNode);
src2.start();
});
}
function promptAndMute(buffer) {
// 1. 正确获取麦克风轨道(使用 LivekitClient 命名空间)
const micPub = room.localParticipant.getTrackPublication(LivekitClient.Track.Source.Microphone);
const micTrack = micPub?.track;
// 2. 瞬时静音 → 播放 → 恢复
if (micTrack) micTrack.mediaStreamTrack.enabled = false;
playPrompt(buffer);
setTimeout(() => {
if (micTrack) micTrack.mediaStreamTrack.enabled = true;
}, 380);
}
//华为杂音处理代码
function handleLocalTrackPublished(publication, participant) { function handleLocalTrackPublished(publication, participant) {
console.log(`本地轨道已发布: ${publication.trackName || publication.source}`); console.log(`本地轨道已发布: ${publication.trackName || publication.source}`);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!