javascript – Safari上是否支持Web Audio API source.start(0)(它在Chrome上运行正常)?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Safari上是否支持Web Audio API source.start(0)(它在Chrome上运行正常)?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2129字,纯文字阅读大概需要4分钟。
内容图文
![javascript – Safari上是否支持Web Audio API source.start(0)(它在Chrome上运行正常)?](/upload/InfoBanner/zyjiaocheng/817/200bcd4dbdb94041a21d51db4b7765dc.jpg)
我正在尝试实现Web Audio API.该代码适用于Chrome 29.0.1547.76,但不适用于Safari 6.0.5(8536.30.1).关键是我是使用noteOn(0)还是start(0).
我想使用start()以便我可以播放声音的一部分:
asource.start(0, 2, 1);
在Chrome中工作正常(立即播放,从2秒开始,播放1秒)但结果却是
TypeError: 'undefined' is not a function (evaluating 'asource.start(0, 2, 1)')
在Safari上.用.替换那一行
asource.noteOn(0);
作品. [好吧,我需要调用noteOff(0)而不是stop(0).]我在start(0)时得到了同样的错误.所以,我假设Safari没有实现start(0)?但如果是这样,为什么在HTML5 Rocks使用start(0)的一些例子工作呢?
作为参考,这是完整的网页.我尝试了很多不同的声音/格式;都会导致同样的错误.
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Web Audio API Issue</title>
</head>
<body>
<p>Example working on Chrome but not Safari when using start()</p>
<button id="Load" onclick="init()" >Load</button>
<button id="Play" onclick="playSound()" disabled>Play</button>
<button id="Stop" onclick="stopSound()" disabled>Stop</button>
<script>
var web_audio_context;
var abuffer;
var asource;
function init() {
contextClass = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.msAudioContext);
web_audio_context = new contextClass();
var theURL = './digits.mp3';
var xhr = new XMLHttpRequest();
xhr.open('GET', theURL, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
finishedLoading(this.response);
};
xhr.send();
}
function finishedLoading(arrayBuffer) {
web_audio_context.decodeAudioData(arrayBuffer, function(buffer) {
abuffer = buffer;
document.getElementById('Load').disabled = true;
document.getElementById('Play').disabled = false;
document.getElementById('Stop').disabled = false;
}, function(e) {
console.log('Error decoding file', e);
});
}
function playSound() {
asource = web_audio_context.createBufferSource();
asource.buffer = abuffer;
asource.connect(web_audio_context.destination);
asource.start(0, 2, 1);
}
function stopSound() {
asource.stop(0);
}
</script>
</body>
</html>
解决方法:
在较新的Web Audio API版本中,方法noteOn重命名为start. Safari仍然使用旧版本,而Chrome使用更新版本.
试一试:
asource.start ? asource.start(0, 2, 1) : asource.noteOn(0, 2, 1);
内容总结
以上是互联网集市为您收集整理的javascript – Safari上是否支持Web Audio API source.start(0)(它在Chrome上运行正常)?全部内容,希望文章能够帮你解决javascript – Safari上是否支持Web Audio API source.start(0)(它在Chrome上运行正常)?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。