基本 Web 音频 API 不播放 mp3 文件
Basic Web Audio API not playing a mp3 file?
我正在尝试通过拼凑示例来在线学习教程。我觉得这应该播放 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方法开始(。
相关文章:
- 使链接播放.mp3文件
- 在网页上连续播放mp3的最有效方式(如网络收音机)
- 播放mp3文件之前,请参阅其他页面的javascript
- 如何使用Javascript播放mp3
- 在安卓上播放mp3的黑客
- 在点击时播放mp3文件与javascript
- 基本 Web 音频 API 不播放 mp3 文件
- 为什么获胜't Jplayer CirclePlayer播放mp3,其他Jplayer皮肤都可以
- 如何在没有任何html5功能的情况下通过javascript播放mp3文件
- 使用javascript播放mp3文件
- 如何在网页中播放mp3
- 播放mp3按钮点击与PRIMEFACES
- 如何在网页上播放.mp3或.m4a音频,同时显示嵌入的专辑艺术?(使用Javascript、HTML5或Flash)
- IE11 HTML5不能播放MP3文件
- 如何检测浏览器类型在HTML5或Javascript播放mp3,而不是在Safari/IE浏览器的webm
- 用soundmanager2播放MP3几秒钟
- 播放MP3文件顺序没有HTML5音频与JavaScript
- 为什么FireFox不能播放mp3文件?
- Firefox无法播放mp3
- 在Firefox中使用howler.js播放mp3