AudioListener

AudioListener interface represents the position and orientation of the unique person listening to the audio scene, and is used in audio spatialization . All PannerNode s spatialize in relation to the AudioListener stored in the BaseAudioContext.listener 属性。

It is important to note that there is only one listener per context and that it isn't an AudioNode .

We see the position, up and front vectors of an AudioListener, with the up and front vectors at 90° from the other.

特性

The position, forward, and up value are set and retrieved using different syntaxes. Retrieval is done by accessing, for example, AudioListener.positionX , while setting the same property is done with AudioListener.positionX.value . This is why these values are not marked read only, which is how they appear in the specification's IDL.

AudioListener.positionX
Represents the horizontal position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.positionY
Represents the vertical position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.positionZ
Represents the longitudinal (back and forth) position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.forwardX
Represents the horizontal position of the listener's forward direction in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.forwardY
Represents the vertical position of the listener's forward direction in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.forwardZ
Represents the longitudinal (back and forth) position of the listener's forward direction in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is -1.
AudioListener.upX
Represents the horizontal position of the top of the listener's head in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.upY
Represents the vertical position of the top of the listener's head in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is 1.
AudioListener.upZ
Represents the longitudinal (back and forth) position of the top of the listener's head in the same cartesian coordinate sytem as the position ( positionX , positionY ,和 positionZ ) values. The forward and up values are linearly independent of each other. The default is 0.

方法

AudioListener.setOrientation()
Sets the orientation of the listener.
AudioListener.setPosition()
Sets the position of the listener.

注意 : Although these methods are deprecated they are currently the only way to set the orientation and position in Firefox, Internet Explorer and Safari.

弃用特征

AudioListener.dopplerFactor
A double value representing the amount of pitch shift to use when rendering a doppler effect .
AudioListener.speedOfSound
Is a double value representing the speed of sound, in meters per second .

In a previous version of the specification, the dopplerFactor and speedOfSound properties and the setPosition() method could be used to control the doppler effect applied to AudioBufferSourceNode s connected downstream — these would be pitched up and down according to the relative speed of the PannerNode AudioListener . These features had a number of problems:

Because of these issues, these properties and methods have been removed.

setOrientation() and setPosition() methods have been replaced by setting their property value equivilents. For example setPosition(x, y, z) can be achieved by setting positionX.value , positionY.value ,和 positionZ.value 分别。

范例

In the following example, you can see an example of how the createPanner() method, AudioListener and PannerNode would be used to control audio spatialisation. Generally you will define the position in 3D space that your audio listener and panner (source) occupy initially, and then update the position of one or both of these as the application is used. You might be moving a character around inside a game world for example, and wanting delivery of audio to change realistically as your character moves closer to or further away from a music player such as a stereo. In the example you can see this being controlled by the functions moveRight() , moveLeft() , etc., which set new values for the panner position via the PositionPanner() 函数。

To see a complete implementation, check out our panner-node example ( view the source code ) — this demo transports you to the 2.5D "Room of metal", where you can play a track on a boom box and then walk around the boom box to see how the sound changes!

Note how we have used some feature detection to either give the browser the newer property values (like AudioListener.forwardX ) for setting position, etc. if it supports those, or older methods (like AudioListener.setOrientation() ) if it still supports those but not the new properties.

// set up listener and panner position information
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var xPos = Math.floor(WIDTH/2);
var yPos = Math.floor(HEIGHT/2);
var zPos = 295;
// define other variables
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
var panner = audioCtx.createPanner();
panner.panningModel = 'HRTF';
panner.distanceModel = 'inverse';
panner.refDistance = 1;
panner.maxDistance = 10000;
panner.rolloffFactor = 1;
panner.coneInnerAngle = 360;
panner.coneOuterAngle = 0;
panner.coneOuterGain = 0;
if(panner.orientationX) {
  panner.orientationX.setValueAtTime(1, audioCtx.currentTime);
  panner.orientationY.setValueAtTime(0, audioCtx.currentTime);
  panner.orientationZ.setValueAtTime(0, audioCtx.currentTime);
} else {
  panner.setOrientation(1,0,0);
}
var listener = audioCtx.listener;
if(listener.forwardX) {
  listener.forwardX.setValueAtTime(0, audioCtx.currentTime);
  listener.forwardY.setValueAtTime(0, audioCtx.currentTime);
  listener.forwardZ.setValueAtTime(-1, audioCtx.currentTime);
  listener.upX.setValueAtTime(0, audioCtx.currentTime);
  listener.upY.setValueAtTime(1, audioCtx.currentTime);
  listener.upZ.setValueAtTime(0, audioCtx.currentTime);
} else {
  listener.setOrientation(0,0,-1,0,1,0);
}
var source;
var play = document.querySelector('.play');
var stop = document.querySelector('.stop');
var boomBox = document.querySelector('.boom-box');
var listenerData = document.querySelector('.listener-data');
var pannerData = document.querySelector('.panner-data');
leftBound = (-xPos) + 50;
rightBound = xPos - 50;
xIterator = WIDTH/150;
// listener will always be in the same place for this demo
if(listener.positionX) {
  listener.positionX.setValueAtTime(xPos, audioCtx.currentTime);
  listener.positionY.setValueAtTime(yPos, audioCtx.currentTime);
  listener.positionZ.setValueAtTime(300, audioCtx.currentTime);
} else {
  listener.setPosition(xPos,yPos,300);
}
listenerData.innerHTML = 'Listener data: X ' + xPos + ' Y ' + yPos + ' Z ' + 300;
// panner will move as the boombox graphic moves around on the screen
function positionPanner() {
  if(panner.positionX) {
    panner.positionX.setValueAtTime(xPos, audioCtx.currentTime);
    panner.positionY.setValueAtTime(yPos, audioCtx.currentTime);
    panner.positionZ.setValueAtTime(zPos, audioCtx.currentTime);
  } else {
    panner.setPosition(xPos,yPos,zPos);
  }
  pannerData.innerHTML = 'Panner data: X ' + xPos + ' Y ' + yPos + ' Z ' + zPos;
}
					

注意 : In terms of working out what position values to apply to the listener and panner, to make the sound appropriate to what the visuals are doing on screen, there is quite a bit of math involved, but you will soon get used to it with a bit of experimentation.

规范

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

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioListener Chrome 14 Edge ≤18 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
dopplerFactor
弃用
Chrome 不支持 14 — 56 Edge 不支持 12 — 79 Firefox 不支持 25 — 63 IE 不支持 No Opera 不支持 15 — 43 Safari 6 WebView Android 不支持 ? — 56 Chrome Android 不支持 18 — 56 Firefox Android 不支持 26 — 63 Opera Android 不支持 14 — 43 Safari iOS ? Samsung Internet Android 不支持 1.0 — 6.0
forwardX Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
forwardY Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
forwardZ Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
positionX Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
positionY Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
positionZ Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
setOrientation
弃用 非标
Chrome 14 Edge 18 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
setPosition
弃用 非标
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
speedOfSound
弃用
Chrome 不支持 14 — 56 Edge 不支持 12 — 79 Firefox 不支持 25 — 63 IE 不支持 No Opera 不支持 15 — 43 Safari 6 WebView Android 不支持 ? — 56 Chrome Android 不支持 18 — 56 Firefox Android 不支持 26 — 63 Opera Android 不支持 14 — 43 Safari iOS ? Samsung Internet Android 不支持 1.0 — 6.0
upX Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
upY Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0
upZ Chrome 52 Edge ≤18 Firefox 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
IE 不支持 No Opera 39 Safari 不支持 No WebView Android 52 Chrome Android 52 Firefox Android 不支持 No
注意事项
No
注意事项
Supports a deprecated way of setting orientation - the setOrientation() 方法。
Opera Android 41 Safari iOS ? Samsung Internet Android 6.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

另请参阅