Javascript 为什么我的音频缓冲区没有';你不会放任何声音吗?[网络音频API]
我的音频缓冲区似乎不工作,我不知道为什么。我已经尝试在Chrome和Safari中打开它,但什么也没发生。我还检查了我的音频文件“Audio2.mp3”是否一切正常Javascript 为什么我的音频缓冲区没有';你不会放任何声音吗?[网络音频API],javascript,audio,buffer,web-audio-api,Javascript,Audio,Buffer,Web Audio Api,我的音频缓冲区似乎不工作,我不知道为什么。我已经尝试在Chrome和Safari中打开它,但什么也没发生。我还检查了我的音频文件“Audio2.mp3”是否一切正常 因为,在定义audioBuffer之前触发playback() 尝试等待音频xhr完全加载,分配audioBuffer,然后执行playback(),它将按预期工作 例如 //创建与旧版Firefox和Chrome浏览器兼容的音频上下文 函数audioContextCheck(){ if(音频上下文的类型!=“未定义”){ 返回新
因为,在定义
audioBuffer
之前触发playback()
尝试等待音频xhr完全加载,分配audioBuffer
,然后执行playback()
,它将按预期工作
例如
//创建与旧版Firefox和Chrome浏览器兼容的音频上下文
函数audioContextCheck(){
if(音频上下文的类型!=“未定义”){
返回新的AudioContext();
}else if(webkitAudioContext的类型!=“未定义”){
返回新的webkitAudioContext();
}else if(mozAudioContext的类型!=“未定义”){
返回新的mozAudioContext();
}否则{
抛出新错误(“不支持AudioContext”);
}
}
var audioContext=audioContextCheck();
//使用XMLHttpRequest为音频文件创建音频缓冲区
无功音频缓冲器;
var getSound=newXMLHttpRequest();
getSound.open(“get”https://cdn.rawgit.com/devildrey33/devildrey33/ddb01d71/Ejemplos/BannerTest/Canciones/LevenRain_-_ActionMan_Versus_The_CyberParasites.mp3“,对);
getSound.responseType=“arraybuffer”;
getSound.onload=函数(){
document.getElementById(“xhrStatus”).textContent=“Loaded”;
audioContext.decodeAudioData(getSound.response,函数(缓冲区){
音频缓冲区=缓冲区;
playback();//@IsaiasHerrera上面的示例可以工作。或者问题可能出在其他地方,比如移动浏览器中的音频权限。它工作了!这是我的问题:我喜欢这首歌
"use strict"
//Create the Audio Context, compatible with older Firefox and Chrome browsers
function audioContextCheck(){
if (typeof AudioContext !== "undefined"){
return new AudioContext();
}
else if (typeof webkitAudioContext !== "undefined") {
return new webkitAudioContext();
}
else if (typeof mozAudioContext !== "undefined") {
return new mozAudioContext();
}
else {
throw new Error('AudioContext not supported');
}
}
var audioContext = audioContextCheck();
//Create audio buffer to the audio file with the XMLHttpRequest
var audioBuffer;
var getSound = new XMLHttpRequest();
getSound.open("get", "Audio2.mp3", true);
getSound.responseType = "arraybuffer";
getSound.onload = function(){
audioContext.decodeAudioData(getSound.response, function(buffer) {
audioBuffer = buffer;
});
};
getSound.send();
//EventListener
window.addEventListener("load", playback);
//Now create the function necessary to play back the audio buffer
function playback(){
var playSound = audioContext.createBufferSource();
playSound.buffer = audioBuffer;
playSound.connect(audioContext.destination);
playSound.start(audioContext.currentTime);
}