Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
李宁
/
Activity
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 6cf9c4e8
authored
Dec 04, 2025
by
李宁
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1
1 parent
cd04d846
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
2 additions
and
115 deletions
huaian/demo.html
huaian/js/demo.js
huaian/demo.html
View file @
6cf9c4e
...
...
@@ -78,17 +78,7 @@
</div>
</div>
<div
class=
"swDown"
id=
"tipDiv"
style=
"display: flex;"
>
<div
class=
"box"
>
<label>
麦克风
</label>
<select
id=
"micSelect"
></select>
</div>
<div
class=
"box"
>
<label>
扬声器
</label>
<select
id=
"spkSelect"
></select>
</div>
<div
class=
"swDown"
id=
"tipDiv"
>
<div
class=
"buttDiv"
style=
"flex-grow: 1;"
>
<div
class=
"one none clickButt"
id=
"readButt"
key=
"ok"
>
准备好了
</div>
...
...
huaian/js/demo.js
View file @
6cf9c4e
...
...
@@ -1035,15 +1035,7 @@ async function init(){
await
this
.
room
.
localParticipant
.
setCameraEnabled
(
true
)
util
.
toast
(
'摄像头开启成功'
)
await
this
.
room
.
localParticipant
.
setMicrophoneEnabled
(
true
,{
echoCancellation
:
true
,
noiseSuppression
:
true
,
autoGainControl
:
true
,
// 关键:把“播放”通道排除掉,避免浏览器混音
channelCount
:
1
,
sampleRate
:
48000
,
})
await
this
.
room
.
localParticipant
.
setMicrophoneEnabled
(
true
)
util
.
toast
(
'麦克风开启成功'
)
// await room.localParticipant.enableCameraAndMicrophone()
...
...
@@ -1082,101 +1074,6 @@ async function init(){
},
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
)
{
console
.
log
(
`本地轨道已发布:
${
publication
.
trackName
||
publication
.
source
}
`
);
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment