- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下非常基本的代码,它是一个更复杂问题的一部分。
我的问题是函数:context.decodeAudioData(arrayBuffer)
在 iPhone
上不工作(在 Safari
和 上试过Chrome
) 或 Mac
(Safari),但它适用于其他任何地方(Android
和 Windows 10
(所有浏览器)。即使它适用于 Mac
(Chrome)。
在 Mac
(Safari) 上我收到以下错误:
Unhandled Promise Rejection: TypeError: Not enough arguments
代码如下:
window.AudioContext = window.AudioContext || window.webkitAudioContext;
const context = new AudioContext();
window.addEventListener('load', function() {
const button_option_1 = document.querySelector('.button_option_1');
const button_option_1_play = document.querySelector('.button_option_1_play');
button_option_1_play.disabled = true;
button_option_1.addEventListener('click', async function() {
log('', false);
let buffer;
button_option_1_play.disabled = true;
button_option_1_play.onclick = () => playBuffer(buffer);
let time_start = new Date().getTime();
let b64 = '//NExAAQAEIUANLGJD7ADozTCghwCrpuImJJfsmVhFye+crPy6UZNvdyC3ixx2QbUXKDDlX08m12kIo4WCJRygGQeQkwibDB/ZrHyhc5cQBEFHBAwIxQ5WKqwiN9YbWw//NExBMQwD5EAOIGBMnKxBJ9D0giPOFBPScfvxIpVJq4mK7+A9D5y8Y3LvMyC1DLUJbcEQI6BAGsF2H1DDl7tCB0gNA0YnTME2djADGlXHxJFDYoGQqNS2+JIwq5AuAZ//NExCMQOD5EAOMEBDwo++j0YiFS9EwDXtsa1iuwlUxFVXUeHNuidz1PGD08yRPht+9y+rq5jvqSGkhjZxskqbnQtm7qTjAFJASA5QYkFmJUDrpc/LwOKoYxbigaWtZp//NExDUReMpAAOBEcCtjm+uvaHV6VmbRdCLU6koVI9x4hWKTSqdtcuzs3G4KinsCsLgxNRBEsBhAqKBImFydfRRBZy4rFc7R7C+57/+heawnfqyxESIRR/hRM/z9qyQK//NExEIWoa5MAMp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Mu//NExOkUKLnAANDGTDk5LjVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVTEFNRTMu//NExKwAAANIAAAAADk5LjVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVTEFNRTMu//NExKwAAANIAAAAADk5LjVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV//NExKwAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV//NExKwAAANIAAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV';
arrayBuffer = base64ToArrayBuffer(b64);
time_start = new Date().getTime();
let audioBuffer = await context.decodeAudioData(arrayBuffer);
let duration = sprintf('%.2fs', (new Date().getTime() - time_start) / 1000);
log(sprintf('P3. Delay: +%s for decoding.', duration));
button_option_1_play.disabled = false;
buffer = audioBuffer;
button_option_1_play.click();
});
});
function playBuffer(buffer, from, duration) {
const source = context.createBufferSource(); // source: AudioBufferSourceNode
source.buffer = buffer;
source.connect(context.destination);
source.start(context.currentTime, from, duration);
}
function log(text, append = true) {
let log = document.querySelector('.log');
if (!append)
log.innerHTML = '';
let entry = document.createElement('div');
entry.innerHTML = text;
log.appendChild(entry);
}
function base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
.log {
display: inline-block;
font-family: "Courier New", Courier, monospace;
font-size: 13px;
margin-top: 10px;
padding: 4px;
background-color: #d4e4ff;
}
.divider {
border-top: 1px solid #ccc;
margin: 10px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/sprintf/1.1.1/sprintf.min.js"></script>
<button class="button_option_1">Option 1</button>
<button class="button_option_1_play">Play</button><br />
<div class="log">[empty]</div>
这里是对应的CodePen.io
:
最佳答案
Safari 不知道基于 promise 的 decodeAudioData
。您将不得不使用回调。
context.decodeAudioData(arrayBuffer, (buffer) => {
resolve(buffer);
}, (e) => { reject(e); });
关于javascript - AudioContext.decodeAudioData(...) 不能在 iPhone 上工作,但在其他任何地方都可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52489787/
我的代码在 Chrome 中运行良好,但现在我认为由于 Chrome 更新,它停止运行了。 var audioContext = new(window.audioContext || window.w
我想在我的游戏中使用麦克风(点击,游戏有音乐和音效,我使用 HTML5 音频元素),如果我添加(仅!)这一行: var audioCtx = new AudioContext (); 并在 Chrom
我正在开发一个 JavaScript 库 ( https://github.com/yvesgurcan/web-midi-player ) 以在 Web 应用程序中启用 MIDI 播放。该库依赖于
我正在开发一个 JavaScript 库 ( https://github.com/yvesgurcan/web-midi-player ) 以在 Web 应用程序中启用 MIDI 播放。该库依赖于
作为这篇文章的后续:How to rapidly play multiple copies of a soundfile in javascript我创建了一个小的演示页面来说明我的问题的核心。 我的
我以前使用过 javascript Audio(),但现在我需要在音频中添加一些混响效果,我正在使用使用 AudioContext api 的 reverb.js。我有可用的开始属性,但没有暂停属性?
我遵循一种模式使用 Webaudio 将声音附加到对象。它运行良好,但是,如果我在一个页面上生成多个项目并附加此脚本,我会收到一个 console.log,表明我已经超过了每页可用的 audioCon
使用 WebAudio API 播放直播流的正确方法是什么。 我正在尝试使用以下代码,但是我看到的只是正在下载 MP3,但没有播放;可能 MediaElementSource 需要一个文件,而不是连续
受此启发SO answer ,我有这段代码,它可以工作,但它会弹出并且听起来不太流畅。 $('.btn').each(initDraggable); function initDraggable()
我有两个一秒音频源,如下所示: var context = system.AudioContext(); var source = context.createBufferSource(); var
我知道你不能多次演奏振荡器,所以我编写了一个函数,每次都会创建一个新的振荡器: function playFrequency(f, t0, t1){ console.log(f, t0, t1
我试图在三星 Galaxy SII 4G 上的最新版本的 Chrome (34.0.1847.114) 中使用 AudioContext(),但由于某种原因 AudioContext() 不存在,we
我已经关注了这个 tutorial并提出该代码: context = new AudioContext(); play(frequency) { const o = this.context.
如果我现在需要在 HTML5 中使用音频 API,我应该使用哪一个?我看到两个可用的 API:Audio API 和 Web Audio API。 基于 this documentation from
我正在构建一个音乐播放器,我想根据当前播放的轨道添加脉动效果。 这里是我想要的一些例子: http://www.htmlfivewow.com/demos/hal/index.html http://
我正在分析一个音频文件,以便使用 channelData 来驱动我的 webapp 的另一部分(基本上是基于音频文件绘制图形)。播放的回调函数如下所示: successCallback(mediaSt
所以,我刚刚发现你可以 record sound using javascript 。太棒了! 我立即创建了一个新项目来自己做一些事情。然而,当我打开示例脚本的源代码时,我发现根本没有任何解释性注释。
我正在寻找一种使用浏览器中的 JavaScript 生成和播放 WAVE 表单的方法。该方法解释如下:http://js.do/blog/sound-waves-with-javascript/ ,非
我正在尝试使用 XMLHttpRequests 和 AudioContext 加载音频,我的代码如下所示: class AudioExample audioContext: null
var audioContext = new window.AudioContext chrome.runtime.onMessage.addListener( function(imageUrl
我是一名优秀的程序员,十分优秀!