Rob*_*ena 6 javascript html5 html5-audio web-audio-api
首先我要描述我的问题,我正在用随机歌曲创建一个自动播放列表,一些歌曲在歌曲结尾处有10-15秒的静音,我想要实现的是从分析仪中检测到当一首歌静止了5秒并对此采取行动时.
到目前为止,我有这个:
var context, analyser, source, audio;
context = new (window.AudioContext || window.webkitAudioContext)();
analyser = context.createAnalyser();
audio = new Audio();
source = context.createMediaElementSource(audio)
source.connect(analyser);
analyser.connect(context.destination);
var playNext = function() {
var pickedSong;
// chooses a song from an api with several
// thousands of songs and store it in pickedSong
audio.src = pickedSong;
audio.play();
}
audio.addEventListener('ended', playNext);
playNext();
Run Code Online (Sandbox Code Playgroud)
我知道答案在分析器的某处,但我没有发现它返回的数据有任何连贯性.
我可以这样做:
var frequencies = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatFrequencyData(frequencies);
Run Code Online (Sandbox Code Playgroud)
并且频率var将包含2048个键,每个键具有随机(对我而言)数字(-48.11,-55,-67等等),这些数字是否表示与所发挥的感知声音有关的任何内容?我发现它是否足够低,人们会认为什么都没有播放.
为了检测我主要想要这样的东西:
var isInSilence = function(){
return !audible;
}
var tries = 0;
var checker = function() {
tries = isInSilence() ? tries + 1 : 0;
if(tries >= 5) playNext();
setTimeout(checker, 1000);
}
checker();
Run Code Online (Sandbox Code Playgroud)
唯一缺失的部分是检测歌曲目前是否无声,任何帮助都将受到赞赏.
编辑:
根据威廉的回答,我设法通过这样做解决了这个问题:
var context, compressor, gain, source, audio;
context = new (window.AudioContext || window.webkitAudioContext)();
compressor = context.createDynamicsCompressor();
gain = context.createGain();
audio = new Audio();
source = context.createMediaElementSource(audio)
// Connecting source directly
source.connect(context.destination);
// Connecting source the the compresor -> muted gain
source.connect(compressor);
compressor.connect(gain);
gain.connect(context.destination);
gain.gain.value = 0; // muting the gain
compressor.threshold.value = -100;
var playNext = function() {
var pickedSong;
// chooses a song from an api with several
// thousands of songs and store it in pickedSong
audio.src = pickedSong;
audio.play();
}
audio.addEventListener('ended', playNext);
playNext();
var isInSilence = function(){
return compressor.reduction.value >= -50;
}
var tries = 0;
var checker = function() {
tries = isInSilence() ? tries + 1 : 0;
if(tries >= 5) playNext();
setTimeout(checker, 1000);
}
checker();
Run Code Online (Sandbox Code Playgroud)
这是使用不同方法(压缩机节点)的可能解决方案。这是一个简短的描述,但应该足以让您填写用例的详细信息:
创建一个压缩器节点并将输入源连接到它。然后将压缩器连接到增益节点并使增益节点静音(将其设置为零)。将增益节点连接到audioContext.destination
获取您的输入源并将其连接到audioContext.destination.
设置压缩器属性值以检测信号(以便触发减少值)。
用或括compressor.reduction.value起以检查更改。setIntervalrequestAnimationFrame
编写当该值更改(或不更改)时执行所需操作所需的逻辑。
| 归档时间: |
|
| 查看次数: |
714 次 |
| 最近记录: |