pan 特性为 StereoPannerNode interface is an a-rate AudioParam representing the amount of panning to apply. The value can range between -1 (full left pan) and 1 (full right pan).

句法

var audioCtx = new AudioContext();
var panNode = audioCtx.createStereoPanner();
panNode.pan.value = -0.5;
					

Returned value

An a-rate AudioParam containing the panning to apply.

注意 : Though the AudioParam returned is read-only, the value it represents is not.

范例

In our StereoPannerNode example ( see source code ) HTML we have a simple <audio> element along with a slider <input> to increase and decrease pan value. In the JavaScript we create a MediaElementAudioSourceNode StereoPannerNode , and connect the two together using the connect() method. We then use an oninput event handler to change the value of the StereoPannerNode.pan parameter and update the pan value display when the slider is moved.

Moving the slider left and right while the music is playing pans the music across to the left and right speakers of the output, respectively.

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var myAudio = document.querySelector('audio');
var panControl = document.querySelector('.panning-control');
var panValue = document.querySelector('.panning-value');
pre.innerHTML = myScript.innerHTML;
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
var source = audioCtx.createMediaElementSource(myAudio);
// Create a stereo panner
var panNode = audioCtx.createStereoPanner();
// Event handler function to increase panning to the right and left
// when the slider is moved
panControl.oninput = function() {
  panNode.pan.setValueAtTime(panControl.value, audioCtx.currentTime);
  panValue.innerHTML = panControl.value;
}
// connect the MediaElementAudioSourceNode to the panNode
// and the panNode to the destination, so we can play the
// music and adjust the panning using the controls
source.connect(panNode);
panNode.connect(audioCtx.destination);
					

规范

规范 状态 注释
Web 音频 API
The definition of 'pan' 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
pan Chrome 41 Edge 12 Firefox 37 IE 不支持 No Opera 28 Safari 不支持 No WebView Android 41 Chrome Android 41 Firefox Android 37 Opera Android 28 Safari iOS 不支持 No Samsung Internet Android 4.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: