visualizer不仅仅出现在safari中。以下代码在其他浏览器中正常工作。 window.AudioContext = window.AudioContext || || window.webkitAudioContext window.mozAudioContext;
function player(){
context=new AudioContext();
analyser=context.createAnalyser();
canvas=document.getElementById('myCanvas');
ctx=canvas.getContext('2d');
var winWidth=$(window).outerWidth(true);
var cal=winWidth*62/100;
var canvasWidth=Math.round(cal);
if(winWidth>992){
bars=150;
ctx.canvas.width=canvasWidth;
}else{
bars=100;
ctx.canvas.width=winWidth-40;
}
ctx.fillRect(20,20,100,100);
gradient=ctx.createLinearGradient(0,0,0,350);
gradient.addColorStop(1,'#000000');
gradient.addColorStop(0.5,'#F40606');
gradient.addColorStop(0,'#186F00');
source=context.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(context.destination);
x=0;
y=210;
w=canvas.width;
frameLooper();
} //播放机
function frameLooper(){
window.requestAnimationFrame(frameLooper);
fbc_array=new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(fbc_array);
ctx.clearRect(0,0,canvas.width,canvas.height);
var logo=new Image();
logo.src="/images/logo3.png";
for(var yo=0;yo<bars;yo++){
bar_x2=yo*8;
bar_width=4;
bar_height2=-(fbc_array[yo]/2);
ctx.fillStyle=gradient;
ctx.fillRect(bar_x2,canvas.height,bar_width,bar_height2);
}
ctx.drawImage(logo,x,y,w,bar_height2*2);
} // frameLooper
var audio,playbtn,mutebtn,seek_bar,volumeslider;
function initAudioPlayer(){
audio=new Audio();
audio.src="/audio/mu.mp3";
audio.loop=true;
player();
audio.play();
audio.volume=0.1;
setTimeout(function(){audio.volume=0.2;},500);
setTimeout(function(){audio.volume=0.3;},500);
setTimeout(function(){audio.volume=0.4;},2000);
setTimeout(function(){audio.volume=0.5;},3000);
setTimeout(function(){audio.volume=0.6;},4000);
setTimeout(function(){audio.volume=0.7;},5000);
setTimeout(function(){audio.volume=0.8;},6000);
setTimeout(function(){audio.volume=0.9;},7000);
setTimeout(function(){audio.volume=1.0;},8000);
if(audio.paused){
$('#logo').fadeIn(500);
$('#canvasWrap').hide();
$('#playPause').removeClass('icon-pause');
$('#playPause').addClass('icon-play');
}else{
$('#logo').hide();
$('#canvasWrap').fadeIn(500);
$('#playPause').removeClass('icon-play');
$('#playPause').addClass('icon-pause');
}
playbtn=document.getElementById('playPause');
playbtn.addEventListener("click",playPause);
function playPause(){
if(audio.paused){
$('#logo').hide();
$('#canvasWrap').fadeIn(500);
$('#playPause').removeClass('icon-play');
$('#playPause').addClass('icon-pause');
// $('#trackInfo').fadeIn(500);
audio.play();
}else{
$('#canvasWrap').hide();
$('#logo').fadeIn(500);
$('#playPause').removeClass('icon-pause');
$('#playPause').addClass('icon-play');
// $('#trackInfo').fadeOut(500);
audio.pause();
}
}//playPause
window.addEventListener("load",initAudioPlayer,true);