跳到主要内容

setVolume 设置音量

setVolume

类型: MethodDeclaration

所属类: MultipleAudioPlayer

定义位置: audio.ts

描述

设置音量

参数 volume: 播放音量[0-1]

参数 easingId: 过渡曲线ID

参数 duration: 持续时间(毫秒)

参数

参数名类型描述默认值
volumenumber--
easingIdstring-''
durationnumber-n0

返回值

类型: void

源代码

位置: 第 509 行

public setVolume(volume: number, easingId: string = '', duration: number = 0): void {
// 如果上一次的音量过渡未结束,移除
if (this.volumeTransition !== null) {
this.volumeTransition.remove()
this.volumeTransition = null
}
const {gain} = this.gain
if (duration > 0) {
const start = gain.value
const end = volume
const easing = Easing.get(easingId)
// 创建音量过渡计时器
this.volumeTransition = new Timer({
duration: duration,
update: timer => {
const time = easing.get(timer.elapsed / timer.duration)
gain.value = Math.clamp(start * (1 - time) + end * time, 0, 1)
},
callback: () => {
this.volumeTransition = null
},
}).add()
} else {
// 直接设置音量
gain.value = Math.clamp(volume, 0, 1)
}
}

文档生成时间:2025/7/7 12:07:06