gpt4 book ai didi

javascript - 基本网络音频 API 不播放 mp3 文件?

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:51 25 4
gpt4 key购买 nike

我正在尝试通过拼凑示例来学习在线教程。我觉得这应该是播放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>

最佳答案

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com