html5声音播放音乐,HTML5 煽情的音乐播放器和音频可视化
JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
var minY = 135; //lowest point
var maxY = 10; //highest point
var pausePosition = 0.75; //position of balls when not playing, percentage
var reverse = false; //reverse lowest and highest
var offsetBalls = true; //mix balls
var smoothFac = 0.85; //value smoothing for ball position
var smoothFacPause = 0.975; //value smoothing when paused
var cutoffLow = 0.1; //percentage of sound spectrum that's cut off from the bottom
var cutoffHigh = 0.65; //percentage of sound spectrum that's cut off from the top
var songUrl = "http://reneroth.org/projects/codepen/dornendiamant.ogg"; //this needs correct Access-Control-Allow-Origin headers
var $c = $('.control');
var isLoaded = false;
var $d = $('.drop');
if (reverse) {
var tmp = minY;
minY = maxY;
maxY = tmp;
}
$c.click(function(e) {
e.preventDefault();
if (!isLoaded) {
if ($c.hasClass('loading')) {
return;
}
$c.addClass('loading');
eq.play(songUrl, function() {
$c.removeClass('loading').addClass('playing');
isLoaded = true;
});
} else {
if ($c.hasClass('playing')) {
$c.removeClass('playing');
eq.stopSound();
} else {
$c.addClass('playing');
eq.replaySound();
}
}
});
var offsets = [];
function doRender() {
requestAnimationFrame(doRender);
$d.each(function(i) {
if (offsetBalls) {
if (i % 2 != 0) {
i = Math.ceil($d.length / 2) + (i - 1) / 2;
} else {
i /= 2;
}
}
if (typeof offsets[i] == 'undefined') {
offsets[i] = 0;
}
var a = eq.getSpectrumByPercentage((i + 0) / ($d.length)) / 255;
if ($c.hasClass('playing')) {
offsets[i] = a * (1 - smoothFac) + offsets[i] * smoothFac;
} else {
a = pausePosition;
offsets[i] = a * (1 - smoothFacPause) + offsets[i] * smoothFacPause;
}
var y = minY - (minY - maxY) * offsets[i];
$(this).css('transform', 'translateY(' + y + 'px)');
});
}
//this part below is taken and modified from Jaakko Alajoki's pen #rVPGBe
var Equalizer = new Function();
if (!window.AudioContext) {
if (!window.webkitAudioContext) {
alert('no audiocontext found');
}
window.AudioContext = window.webkitAudioContext;
}
Equalizer.prototype.context = new AudioContext();
Equalizer.prototype.audioBuffer = [];
Equalizer.prototype.sourceNode = {};
Equalizer.prototype.analyser = {};
Equalizer.prototype.javascriptNode = {};
Equalizer.prototype.audioData = [];
Equalizer.prototype.fftSize = 512;
Equalizer.prototype.playStart = 0;
Equalizer.prototype.playResume = 0;
Equalizer.prototype.buffer = [];
Equalizer.prototype.play = function(url, loadCallback) {
var self = this;
this.javascriptNode = this.context.createScriptProcessor(2048, 1, 1);
this.javascriptNode.connect(this.context.destination);
this.javascriptNode.onaudioprocess = function() {
self.processAudio();
};
this.analyser = this.context.createAnalyser();
this.analyser.smoothingTimeConstant = 0.2;
this.analyser.fftSize = this.fftSize;
this.sourceNode = this.context.createBufferSource();
this.sourceNode.connect(this.analyser);
this.analyser.connect(this.javascriptNode);
this.sourceNode.connect(this.context.destination);
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
self.context.decodeAudioData(request.response, function(buffer) {
self.buffer = buffer;
self.playSound(buffer);
if (typeof(loadCallback) !== "undefined") {
loadCallback();
}
});
}
request.send();
}
Equalizer.prototype.playSound = function(buffer) {
this.sourceNode.buffer = buffer;
this.sourceNode.loop = true;
this.sourceNode.start(0);
this.playStart = new Date().getTime();
}
Equalizer.prototype.stopSound = function() {
this.playResume = new Date().getTime();
this.playResume -= this.playStart;
this.playResume /= 1000;
this.playResume %= this.sourceNode.buffer.duration;
this.sourceNode.stop();
}
Equalizer.prototype.replaySound = function() {
this.sourceNode = this.context.createBufferSource();
this.sourceNode.connect(this.analyser);
this.sourceNode.connect(this.context.destination);
this.sourceNode.buffer = this.buffer;
this.sourceNode.loop = true;
this.sourceNode.start(0, this.playResume);
this.playStart = new Date().getTime() - this.playResume * 1000;
}
Equalizer.prototype.processAudio = function() {
this.audioData = new Uint8Array(this.analyser.frequencyBinCount);
this.analyser.getByteFrequencyData(this.audioData);
}
Equalizer.prototype.getSpectrum = function() {
return this.audioData;
}
Equalizer.prototype.getSpectrumAt = function(i) {
return typeof(this.audioData[i]) !== "undefined" ? this.audioData[i] : 0;
}
Equalizer.prototype.getSpectrumByPercentage = function(p) {
var modLength = this.audioData.length - Math.floor(cutoffLow * this.audioData.length + cutoffHigh * this.audioData.length);
var i = Math.floor(p * modLength + cutoffLow * this.audioData.length);
return typeof(this.audioData[i]) !== "undefined" ? this.audioData[i] : 0;
}
var eq = new Equalizer();
doRender();
html5声音播放音乐,HTML5 煽情的音乐播放器和音频可视化相关推荐
- ffmpeg 音乐循环_自媒体良器:音频文件批处理,FFmpeg一行搞定!果断收藏
引言 FFMpeg 是众多多媒体应用程序的核心,但该程序本身不具备一次转换多个文件的能力. 但也拦不住,FFMpeg 本身是命令行程序,可编写脚本,借助 Bash 轻松快速地构建自动化程序. 1 - ...
- html5 音乐播放进度条,js实现音乐播放控制条
前言 html5中提供audio标签, 该标签实现音频的播放,之前就一直对于音频以及视频播放比较感兴趣,一直想要自己实现一个音频和视频播放的模块,这也是本文章撰写的初衷,最近花了些时间实现了Audio ...
- html在线播放mp3代码实现,实现音乐播放器的代码(html5+css3+jquery)
看下面的效果图很不错吧,是怎么实现的呢?下面小编给大家分享下我的一番宝物,Lisa唱的 在angel beats的插曲.用到html5.css.jquery实现此音乐播放器. 一番宝物,Lisa唱的 ...
- HTML5期末大作业:简单全屏音乐个人网站模板——音乐娱乐网站(6页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码
HTML5期末大作业:简单全屏音乐个人网站模板--音乐娱乐网站(6页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码 常见网页 ...
- HTML+CSS+JS网页设计期末课程大作业 :音乐在线网站设计——音乐在线听平台网站(26页) 网页设计成品DW静态网页Html5响应式css3
HTML5期末大作业:音乐在线网站设计--音乐在线听平台网站(26页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码 常见网页 ...
- 为什么计算机播放音乐不响,酷狗音乐开启以后为什么点选播放没有声音
酷狗音乐开启以后为什么点选播放没有声音以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 酷狗音乐开启以后为什么点选播放没有 ...
- 虾米播播音乐墙html,WordPress音乐播放器插件Hermit X(支持网易云、QQ音乐、虾米等)...
Hermit X,使用 APlayer 前端播放器,Meting Framework & LWL API 后端支持的全新 WordPress 播放器 现已问世! 特性 支持直接调用网易云音乐. ...
- 直播弹幕 html5,一种基于HTML5的弹幕播放器及其方法与流程
本发明涉及互联网技术领域,具体涉及一种基于HTML5的弹幕播放器及其方法. 背景技术: 弹幕是指一种在观看视频时,大量以字幕(有且不限于文字和图片)形式呈现的评论与视频同在一个画面的现象.在视频上方大 ...
- html5 audio 获取播放时间,html5 audio 延时获取播放路径播放失败
为什么audio对象在延时1000毫秒之后就不能执行播放呢? 歌曲的路径需要从数据库中获取,所以需要用ajax来交互,但是发现假如时间过长即使获取到路径也不能执行播放. 为方便测试使用setTimeo ...
最新文章
- C#之IComparable用法,实现ListT.sort()排序
- Angular应用里的@Input和@Output注解使用方法介绍
- matlab中欠定方程组超定方程组_七年级下册第10章:认识二元一次方程组(1课时)...
- 信号分析中一些特征量
- linux内核分成如下五个子系统,linux内核主要由5个子系统 Linux内核由哪几个子系统组成?...
- “Hello,Github!——如何配置并上传一个已有项目到Git上
- java接口测试工具_【分享】接口工具对比(apipost、jmeter、postman、swagger等)
- Thrift 教程 开发 笔记 原理 资料 使用 范例 示例 应用
- 52个有效方法(1) - 了解Objective-C语言的起源
- robot motion planning介绍
- 移动端line-height与height相同文字不居中
- 初步用Echarts实现圆饼图
- WIZ ConfigTool-批量配置WIZnet S2E模块
- EDK2编译环境搭建、编译、在模拟器运行、在笔记本运行
- uClinux 启动过程详细分析
- Gif表情包如何用视频制作?教你一键快速制作gif表情包
- webInspect SprinBoot2.x安全整改
- (一)城市三维基础展示方案
- 谷歌彻底放弃OKR了
- 【编码译码】基于matlab HDB3编译码仿真【含Matlab源码 1961期】
热门文章
- 电商视觉:焦点图的万能构图模板
- 离散信号内插matlab,离散信号和系统实验报告.doc
- .anonymous springsecurity需要登陆嘛_springSecurity之java配置篇
- C++编译原理 (转载)
- cuSPARSE库:(六)cusparseMatrixType_t的说明
- IQ数据简介:I/Q Data
- source insight 深色主题配置文件
- java定时器每一分钟执行一次_2行代码搞定一个定时器
- 最好电脑操作系统_操作系统都有哪些呢
- MapJoin的原理及案例