如何控制浏览器';通过JavaScript访问卷?
我想通过JavaScript控制音量 我发现了如何控制音频对象。 但是,我不知道如何控制音量的设置如何控制浏览器';通过JavaScript访问卷?,javascript,audio,volume,Javascript,Audio,Volume,我想通过JavaScript控制音量 我发现了如何控制音频对象。 但是,我不知道如何控制音量的设置 您能告诉我如何通过JavaScript或一些好的模块控制音量吗?以下控制音量的示例来自Mozilla的使用HTML5音频和视频指南: 以下是一些javascript,它为浏览器提供了一个小部件,可以在呈现音频时更改音量。。。只需更新下面的内容,并用一些音频文件(mp3)填充变量audio_url,将其放入与下面代码相同的目录中 <html> <head> <
您能告诉我如何通过JavaScript或一些好的模块控制音量吗?以下控制音量的示例来自Mozilla的使用HTML5音频和视频指南:
以下是一些javascript,它为浏览器提供了一个小部件,可以在呈现音频时更改音量。。。只需更新下面的内容,并用一些音频文件(mp3)填充变量audio_url,将其放入与下面代码相同的目录中
<html>
<head>
<title>render audio with volume control</title>
</head>
<body>
<p>Volume</p>
<input id="volume" type="range" min="0" max="1" step="0.1" value="0.5"/>
<script>
var audio_context = null;
var gain_node = null;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audio_context = new AudioContext();
gain_node = audio_context.createGain(); // Declare gain node
gain_node.connect(audio_context.destination); // Connect gain node to speakers
function render_audio() {
var request = new XMLHttpRequest();
var audio_url = "your_music.mp3";
request.open('GET', audio_url, true); // loading local file for now
request.responseType = 'arraybuffer';
// Decode asynchronously
request.onload = function() {
audio_context.decodeAudioData(request.response, function(buffer) {
stored_buffer = buffer; // store buffer for replay later
var source = audio_context.createBufferSource(); // creates a sound source
source.buffer = buffer; // tell the source which sound to play
source.connect(gain_node); // connect source to speakers
source.start(0); // play the source now
});
};
request.send();
}
// --- enable volume control for output speakers
document.getElementById('volume').addEventListener('change', function() {
var curr_volume = this.value;
gain_node.gain.value = curr_volume;
console.log("curr_volume ", curr_volume);
});
// init
render_audio();
</script>
<body onkeypress="render_audio()">
<button onclick="render_audio()">play_again</button>
</body>
</html>
使用音量控制渲染音频
卷
var audio_context=null;
var gain_node=null;
window.AudioContext=window.AudioContext | | window.webkitadiocontext;
音频上下文=新的音频上下文();
增益节点=音频上下文。createGain();//声明增益节点
增益节点连接(音频上下文目标);//将增益节点连接到扬声器
函数render_audio(){
var request=new XMLHttpRequest();
var audio_url=“your_music.mp3”;
request.open('GET',audio_url,true);//现在正在加载本地文件
request.responseType='arraybuffer';
//异步解码
request.onload=函数(){
音频上下文解码音频数据(请求、响应、函数(缓冲区){
存储的\u buffer=buffer;//存储缓冲区以便以后重播
var source=audio_context.createBufferSource();//创建一个声音源
source.buffer=buffer;//告诉源要播放的声音
source.connect(增益节点);//将源连接到扬声器
source.start(0);//立即播放源代码
});
};
request.send();
}
//---为输出扬声器启用音量控制
document.getElementById('volume')。addEventListener('change',function(){
var curr_volume=此值;
增益节点。增益值=当前音量;
日志(“当前卷”,当前卷);
});
//初始化
渲染音频();
再玩一次
我想OP知道怎么做。问题是如何控制浏览器的音量?谢谢你的评论。我已经更新了这篇文章,添加了一个讨论JavaScript无法设置或读取系统卷的链接。
<html>
<head>
<title>render audio with volume control</title>
</head>
<body>
<p>Volume</p>
<input id="volume" type="range" min="0" max="1" step="0.1" value="0.5"/>
<script>
var audio_context = null;
var gain_node = null;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audio_context = new AudioContext();
gain_node = audio_context.createGain(); // Declare gain node
gain_node.connect(audio_context.destination); // Connect gain node to speakers
function render_audio() {
var request = new XMLHttpRequest();
var audio_url = "your_music.mp3";
request.open('GET', audio_url, true); // loading local file for now
request.responseType = 'arraybuffer';
// Decode asynchronously
request.onload = function() {
audio_context.decodeAudioData(request.response, function(buffer) {
stored_buffer = buffer; // store buffer for replay later
var source = audio_context.createBufferSource(); // creates a sound source
source.buffer = buffer; // tell the source which sound to play
source.connect(gain_node); // connect source to speakers
source.start(0); // play the source now
});
};
request.send();
}
// --- enable volume control for output speakers
document.getElementById('volume').addEventListener('change', function() {
var curr_volume = this.value;
gain_node.gain.value = curr_volume;
console.log("curr_volume ", curr_volume);
});
// init
render_audio();
</script>
<body onkeypress="render_audio()">
<button onclick="render_audio()">play_again</button>
</body>
</html>