我有这个错误:
XMLHttpRequest cannot load file:///home/......../Web/Sykbox/undefined. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我知道是因为同源策略,但我想知道是否有一个加载localfiles的方法。 这是我的代码:
HTML:
<html>
<head>
<title> Sykbox </title>
<link href="styles/syk.css" rel="stylesheet" type="text/css" />
</head>
<body>
<input type = "button" onclick = "loadDog();" value = "Play" />
<script src = "scripts/sykAudio.js"></script>
</body>
JS:
var audioCtx;
var bufferDog;
var url = "../data/TaktofWind.ogg";
window.addEventListener('load', init, false);
function init() {
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioCtx = new AudioContext();
}
catch(e) {
alert("Web Audio API no es soportado por tu navegador");
}
}
function loadDog(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
audioCtx.decodeAudioData(request.response, function(buffer) { bufferDog = buffer;
playSound(bufferDog); }, function(error) {
console.error("decodeAudioData error", error); } );
};
request.send();
}
function playSound(buffer) {
var source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start(0);
}