我试图通过将示例拼凑在一起来遵循在线教程。我觉得这应该在播放mp3文件。我正在使用Chrome浏览器,并且它是最新的。我在控制台上没有任何错误。我不确定要进行这项工作需要更改或添加的内容。


<script type="text/javascript">

//creating an audio context

window.addEventListener('load',init);

function init()
{

    try
    {
        window.AudioContext = window.AudioContext || window.webkitAudioContext;
        var context=new AudioContext();

    }
    catch(e)
    {
        alert("Your browser doesn't support Web Audio API");
    }

    loadSound();
    playSound();
}

//loading sound into the created audio context

function loadSound()
{
    //set the audio file's URL
    var audioURL='audio files/song.mp3';

    //creating a new request
    var request = new XMLhttpRequest();
    request.open("GET",audioURL,true);
    request.responseType= 'arraybuffer';
    request.onLoad funtion(){

        //take the audio from http request and decode it in an audio buffer

        var audioBuffer = null;

        context.decodeAudioData(request.response, function(buffer){ audioBuffer= buffer;});

    }

    request.send();

}, onError);

//playing the audio file
function playSound(buffer) {
  //creating source node
  var source = audioContext.createBufferSource();
  //passing in file
  source.buffer = audioBuffer;

  //start playing
  source.start(0);
}

</script>

</head>


</html>

最佳答案

您正在使用异步XMLHttpRequest(请注意,它应该用大写的“H”拼写),因此很可能在playSound(注意:缺少request.onLoad)完成之前调用了=函数。

尝试使用console.log或类似方法跟踪脚本的执行情况,以查找类似的错误,并使用JavaScript控制台捕获语法错误。

关于javascript - 基本的Web音频API无法播放mp3文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27589179/

10-11 15:04