Chrome 目前支持 Web Audio API ( http://www.w3.org/TR/webaudio/ ),它有一个可以设置的playbackRate audioParam。它不像<audio>
标签那么简单,但允许各种很酷的东西。我目前正在使用它来处理音高变换/时间拉伸失真。
这是您可以执行的操作的示例:
//build a request and fire it off
speedChanger.loader = (function(){
var _request = new XMLHttpRequest(),
_handleRequest = function(url){
_request.open('GET',url,true);
_request.responseType = 'arraybuffer';
_request.onload = function(){
SpeedChanger.audioGraph.context.decodeAudioData(_request.response, function(buffer){
_loadedBuffer = buffer;
SpeedChanger.audioGraph.setBuffer(buffer);
SpeedChanger.audioGraph.setBufferCache(buffer);
},function(){//error stuff});
};
_request.send();
};
_handleRequest("audio/file.mp3");
}());//loader
grainTable.audioGraph = (function(){
var _context = new webkitAudioContext(), //this is the container for your entire audio graph
_source = _context.createBufferSource(), //your buffer will sit here
_bufferCache, //buffer needs to be re-initialized before every play, so we'll cache what we've loaded here
//for chaching / retrieving the buffer
_getBufferCache = function(){
return _bufferCache;
},
_setBufferCache = function(_sound){
_bufferCache = _sound;
},
//for setting the current instance of the buffer
_setBuffer = function(_sound){
_source.buffer = _sound;
},
_setPlaybackRate = function(rate){
_source.playbackRate.value = rate;
},
_setRate = function(myRate){
_rate = myRate;
}
//play it
_playSound = function(){
_source.noteOff(0); //call noteOff to stop any instance already playing before we play ours
_source = _context.createBufferSource(); //init the source
_setBuffer(_bufferCache); //re-set the buffer
_setPlaybackRate(_rate); //here's your playBackRate check
_source.connect(_context.destination); //connect to the speakers
_source.noteOn(0); //pass in 0 to play immediately
},
}
return{
context :_context,
setBuffer :_setBuffer,
setBufferCache :_setBufferCache,
playSound :_playSound,
setRate :_setRate
}
}());//audioGraph