HTML5音频可视化
HTML5 Audio Visualization?
我看到了一个关于Web Audio API的可视化页面,其中解释了如何制作可视化。我尝试了确切的代码,得到了"ReferenceError: stream is not defined"等错误。是否有一种方法可以从音频元素中制作出可视效果?只有在FireFox上运行才重要。
简单的方法是:
let audio = document.getElementById("yourAudioElement");
window.AudioContext = window.AudioContext || window.webkitAudioContext;
let ctx = new window.AudioContext();
// add the audio element node as source for AudioContext instance
let sourceNode = ctx.createMediaElementSource(audio);
// if you wish to visualize the audio then you will have to connect this to an analyzer
let analyzer = ctx.createAnalyser();
analyser.smoothingTimeConstant = 0.6;
analyser.fftSize = 512; // frequency sample size
// set frequency data
let frequencyData = new Uint8Array(analyser.frequencyBinCount);
// connect source node to analyser to collect frequency data
sourceNode.connect(this.analyser);
// connect this source to the audio output (your speaker device)
sourceNode.connect(ctx.destination);
// you can then start your audio as below
audio.play();
// then you can animate the frequence data by calling requestAnimationFrame()
// this method allows you to keep updating your visualization by frames
function renderFrame() {
// you need to check whether audio is playing here to control this recursive call
requestAnimationFrame(renderFrame);
analyser.getByteFrequencyData(frequencyData);
// then just use your imagination to animate the frequency data by frame
// you can update your canvas here
}
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// Create the instance of AudioContext
var context = new AudioContext();
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
// Access microphone
var medias = {audio : true, video : false};
/**
* @param {MediaStream|LocalMediaStream} stream
*/
var successCallback = function(stream) {
// Create the instance of MediaStreamAudioSourceNode
var source = context.createMediaStreamSource(stream);
// code for visualization
// do something ...
};
/**
* @param {NavigatorUserMediaError|MediaStreamError} error
*/
var errorCallback = function(error) {
console.error(error);
};
navigator.getUserMedia(medias, successCallback, errorCallback);
XSound.js是一个非常有用的Web Audio API库。
相关文章:
- 如何在谷歌可视化中组合数字和模式格式化程序
- 加载泰坦尼克号可视化数据时出现问题
- 如何使用图表包装函数有条件地格式化谷歌可视化表单元格
- AngularJS&Google可视化-使用状态更改处理程序(Google API)在HTML页面上查看$sco
- Javascript图形可视化工具包,具有高性能(500-1000个节点)
- Google可视化addListener调用函数引用错误
- 如何在web上动态可视化数据集
- 可视化到选择框中
- 从特定系列的谷歌可视化折线图中删除悬停工具提示
- 谷歌图表可视化仪表板数据源问题
- 在可视化中将CSS从悬停更改为单击
- 使用 d3 可视化快速射击的“点击”
- 无论如何,可以使用网络音频 API 可视化来自 iframe 的 youtube 音频
- 如何为音频播放器创建可视化工具
- 音频可视化工具C#
- 如何将JavaScript动画与歌曲的节奏同步,而无需构建“音频可视化器”
- HTML5音频可视化
- 预计算Web音频API时域和频谱图可视化
- SoundCloud的可视化音频's自定义播放器
- 音频中的JavaScript波形可视化