setValueAtTime() 方法在 AudioParam interface schedules an instant change to the AudioParam value at a precise time, as measured against AudioContext.currentTime . The new value is given in the value parameter.

句法

var AudioParam = AudioParam.setValueAtTime(value, startTime)
					

参数

A floating point number representing the value the AudioParam will change to at the given time.

startTime
A double representing the time (in seconds) after the AudioContext was first created that the change in value will happen. A TypeError is thrown if this value is negative.

返回

A reference to this AudioParam object. In some browsers older implementations of this interface return void.

范例

This simple example features a media element source with two control buttons (see our webaudio-examples repo for the source code, or view the example live ). When the buttons are pressed, the currGain variable is incremented/decremented by 0.25, then the setValueAtTime() method is used to set the gain value equal to currGain , one second from now ( audioCtx.currentTime + 1 )。

// create audio context
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
// set basic variables for example
var myAudio = document.querySelector('audio');
var pre = document.querySelector('pre');
var myScript = document.querySelector('script');
pre.innerHTML = myScript.innerHTML;
var targetAtTimePlus = document.querySelector('.set-target-at-time-plus');
var targetAtTimeMinus = document.querySelector('.set-target-at-time-minus');
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
var source = audioCtx.createMediaElementSource(myAudio);
// Create a gain node and set it's gain value to 0.5
var gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5;
var currGain = gainNode.gain.value;
// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
// set buttons to do something onclick
targetAtTimePlus.onclick = function() {
  currGain += 0.25;
  gainNode.gain.setValueAtTime(currGain, audioCtx.currentTime + 1);
}
targetAtTimeMinus.onclick = function() {
  currGain -= 0.25;
  gainNode.gain.setValueAtTime(currGain, audioCtx.currentTime + 1);
}
					

规范

规范 状态 注释
Web 音频 API
The definition of 'setValueAtTime' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setValueAtTime Chrome 14 Edge 12 Firefox 25 IE No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: