基本 Web 音频 API 不播放 mp3 文件

Basic Web Audio API not playing a mp3 file?

本文关键字:播放 mp3 文件 API Web 音频 基本      更新时间:2023-09-26

我正在尝试通过拼凑示例来在线学习教程。我觉得这应该播放 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 控制台捕获语法错误。

我把这个问题修好了:)我使用了音频标签以及网络音频API。这是代码:

var audio = new Audio();
audio.src = 'audio files/song.mp3';
audio.controls = true;
audio.autoplay = true;
document.body.appendChild(audio);
var context = new webkitAudioContext();
var analyser = context.createAnalyser();

window.addEventListener('load', function(e) {
  // Our <audio> element will be the audio source.
  var source = context.createMediaElementSource(audio);
  source.connect(analyser);
  analyser.connect(context.destination);

}, false);

感谢试图帮助:)(

您的音频URL是否正确?

audio files/song.mp3 为什么会有空白?

============编辑

==

============
<script>
//creating an audio context
var context;
var audioBuffer;
window.addEventListener('load', init);    
function init()
{
    try
    {
        window.AudioContext = window.AudioContext || window.webkitAudioContext;
        context=new AudioContext();
    }
    catch(e)
    {
        alert("Your browser doesn't support Web Audio API");
    }
    loadSound();
    // playSound();  // comment here
}
//loading sound into the created audio context
function loadSound()
{
    // set the audio file's URL
    var audioURL='AllofMe.mp3';
    //creating a new request
    var request = new XMLHttpRequest();
    request.open("GET",audioURL,true);
    request.responseType= 'arraybuffer';
    request.onload = function(){
        //take the audio from http request and decode it in an audio buffer
        context.decodeAudioData(request.response, function(buffer){
          audioBuffer = buffer;
          console.log(audioBuffer);
          if(audioBuffer){  // check here
            playSound();
          }
        });
    };
    request.send();
}

//playing the audio file
function playSound() {
    //creating source node
    var source = context.createBufferSource();
    //passing in file
    source.buffer = audioBuffer;
    //start playing
    source.connect(context.destination);  // added
    source.start(0);
    console.log('playing');
}
</script>

我一直在寻找在移动设备上播放 mp3 的解决方案,并找到了此页面,我提供了示例以在此处的帮助下工作。提供以下工作示例:

var context;
var saved;
try {
    context = new (window.AudioContext || window.webkitAudioContext)();
}
catch (e) {
    console.log("Your browser doesn't support Web Audio API");
}
if (saved) {
    playSound(saved);
} else {
    loadSound();
}
//loading sound into the created audio context
function loadSound() {
    //set the audio file's URL
    var audioURL = '/path/to/file.mp3';
    //creating a new request
    var request = new XMLHttpRequest();
    request.open('GET', audioURL, true);
    request.responseType = 'arraybuffer';
    request.onload = function () {
        //take the audio from http request and decode it in an audio buffer
        context.decodeAudioData(request.response, function (buffer) {
            // save buffer, to not load again
            saved = buffer;
            // play sound
            playSound(buffer);
        });
    };
    request.send();
}
//playing the audio file
function playSound(buffer) {
    //creating source node
    var source = context.createBufferSource();
    //passing in data
    source.buffer = buffer;
    //giving the source which sound to play
    source.connect(context.destination);
    //start playing
    source.start(0);
}

看起来在Android设备上播放文件可以正常工作,但不能在iOS上工作。为此,您必须遵循本指南:如何:iOS上的Web音频(但使用touchend事件并替换noteOn方法开始(。