iOS 6 Web Audio API没有声音 [英] No sound on iOS 6 Web Audio API

查看:405
本文介绍了iOS 6 Web Audio API没有声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴看到iOS 6支持Web Audio API,因为我们制作了HTML5游戏。但是,我无法让iOS 6使用Web Audio API播放任何声音,并且可以在桌面版Chrome中正常运行。



这是一款带有触摸控件的HTML5游戏并通过Web Audio API播放音频(如果存在 - 如果不存在,它将回退到HTML5音频):



http://www.scirra.com/labs/sbios6b/



编辑:@Srikumar建议了一些解决方法。我在下面的版本中应用它们。它仍然不起作用!



http://www.scirra.com/labs/sbios6f/



桌面版Chrome浏览器的所有功能都很好,但iOS 6完全没有声音。我无法调试它,因为我只进行Windows开发,iOS 6用远程Web检查器取代了调试模式,这显然在Safari for Windows上不可用。使用一些警报,我发现它正确地识别Web Audio API,使用它,检测到没有Vorbis支持,因此退回到AAC音频,解码缓冲区然后播放它,并且没有错误,但是我什么都没听到。当然,我尝试将音量调高到最大值:)



应该没有编解码器问题,因为iOS 6可以很好地播放AAC - 你可以浏览游戏中播放的.m4a之一并且播放效果很好来自Safari。

查看iOS 6上的Web Audio API示例: http://chromium.googlecode.com/svn/trunk/samples/audio/samples.html - 其中一些工作,其他人不工作。例如, Chrome音频可视化工具适用,但 Javascript Drone 没有。



必须有一些微妙的iOS 6上的Web Audio与桌面Chrome之间不兼容。我错过了什么?

解决方案

编辑(2015年11月): touchstart 事件开始,这打破了下面的解决方案。然而它在 touchend 事件中起作用。 iOS 6的原始答案保持不变,但对于iOS 9支持,请确保使用 touchend



那么,很抱歉回答我自己的赏金问题,但经过几个小时的调试后,我终于找到了答案。 iOS 6上的Safari有效地从Web Audio API静音开始。直到您试图在用户输入事件中播放声音时才会取消静音(创建缓冲区来源,将其连接到目标并调用 noteOn())。在此之后,它会取消静音并且音频播放不受限制,并且应该如此。这是关于Web Audio API如何在iOS 6上运行的无证方面(苹果的文档在这里,希望他们能及时更新它。)



用户可以触摸屏幕很多,从事这个游戏。但它将保持静音。对于iOS用户输入事件, 9+],一次,然后所有音频取消静音。之后,您可以随时播放音频(不必在用户输入事件中)。



请注意,这与HTML5音频的限制不同:通常您只能在用户输入事件中启动音频,并且一次只播放一个声音; Web Audio API在第一次播放用户输入后完全取消静音,这样您就可以随时播放声音,然后您可以将它们混合在一起,处理很酷的效果等等。

这意味着许多已经在网络上使用Web Audio API的游戏将永远不会播放音频,因为它们不会在触摸事件中发出noteOn。你必须调整它以等待第一个用户输入事件。



有几种方法可以解决这个问题:在用户触摸之前不要播放你的标题音乐屏幕;有一个初始的触摸启用音频屏幕并播放声音,然后在他们触摸时开始游戏;等等。希望这可以帮助其他有相同问题的人省下一些时间来尝试调试它!


I was really excited to see iOS 6 supports the Web Audio API, since we make HTML5 games. However, I cannot get iOS 6 to play any sound at all using the Web Audio API with examples that work fine in desktop Chrome.

Here is a HTML5 game with touch controls and playing audio via the Web Audio API (if present - if not it will fall back to HTML5 audio):

http://www.scirra.com/labs/sbios6b/

Edit: @Srikumar suggested some workarounds. I applied them at the version below. It still does not work!

http://www.scirra.com/labs/sbios6f/

Everything plays just fine on desktop Chrome, but iOS 6 emits no sound at all. I'm having trouble debugging it because I only do Windows development, and iOS 6 replaced the debug mode with remote web inspector, which apparently is not available on Safari for Windows. Using a few alerts I did find it correctly identifies the Web Audio API, uses it, detects no Vorbis support so falls back to AAC audio, decodes a buffer and then plays it, and no errors are thrown, but I hear nothing. And, of course, I tried turning the volume up to max :)

There should not be a codec problem, because iOS 6 can play AAC just fine - you can browse to one of the .m4a's the game plays and it plays fine visited direct from Safari.

Looking at the Web Audio API examples here on iOS 6: http://chromium.googlecode.com/svn/trunk/samples/audio/samples.html - some of them work, and others don't. For example, the Chrome Audio Visualizer works, but Javascript Drone doesn't.

There must be some subtle incompatibility between Web Audio on iOS 6 and desktop Chrome. What am I missing?

解决方案

Edit (November 2015): iOS 9 no longer allows audio to start in a touchstart event, which breaks the solution below. However it works in a touchend event. The original answer for iOS 6 is left intact below, but for iOS 9 support make sure you use touchend.

Well, sorry to answer my own bounty question, but after hours of debugging I finally found the answer. Safari on iOS 6 effectively starts with the Web Audio API muted. It will not unmute until you attempt to play a sound in a user input event (create a buffer source, connect it to destination, and call noteOn()). After this, it unmutes and audio plays unrestricted and as it ought to. This is an undocumented aspect of how the Web Audio API works on iOS 6 (Apple's doc is here, hopefully they update it with a mention of this soon!)

The user can be touching the screen a lot, engaged in the game. But it will remain muted. You have to play inside a user input event like touchstart [edit: touchend for iOS 9+], once, then all audio unmutes. After that you can play audio at any time (doesn't have to be in a user input event).

Note this is different to the restrictions on HTML5 audio: typically you can only start audio at all in a user input event, and only play one sound at a time; the Web Audio API fully unmutes after the first play-in-user-input, so that you can play sounds at any time, and then you can mix them polyphonically, process cool effects, etc.

This means many games already on the web using the Web Audio API will never play audio, because they do not happen to issue a noteOn in a touch event. You have to adjust it to wait for the first user input event.

There are a few ways to work around this: don't play your title music until the user touches the screen; have an initial 'touch to enable audio' screen and play a sound then begin the game when they touch; etc. Hopefully this will help anyone else having the same problem save some time trying to debug it!

这篇关于iOS 6 Web Audio API没有声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆