在移动Safari浏览器自动播放音频 [英] Autoplay audio on mobile safari

查看:2138
本文介绍了在移动Safari浏览器自动播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我被火烧死,我知道这并不目前由于苹果的关切可怕的成本他们会自动下载音频文件的用户的工作(无关,与他们试图削弱Web应用程序,因为它们穿上 ŧ从他们那里得到他们的30%)。

Before I get flamed to death, I know this doesn't work currently due to Apple's concern over the terrifying cost to their users of downloading an audio file automatically (nothing to do with them trying to cripple web apps as they don't get their 30% from them).

不过,我的问题是:有没有人发现了一个狡猾的解决办法了吗?我只是想玩一开始声音就推出了游戏,目前还要等待用户点击某个地方之前,我可以播放音频。一个聪明的你的CHAPS必须由现在已经得到这方面的工作?

However, my question is: Has anyone found a cunning workaround yet? I just want to play a start up sound on the launch of a game and currently have to wait for the user to click somewhere before I can play the audio. One of you clever chaps must have got this working by now?

推荐答案

有没有机会得到自动播放在移动浏览器的工作。 Android和iOS不允许它,我个人认为这是一个可行的约束!试想一下,每一秒钟的网站,你会在启动时打开的戏剧和丑陋的声音!

There is no chance to get autoplay working in mobile browsers. Android and iOS doesn't allow it and personally I think that is a feasible confinement! Imagine every second website you will open plays and ugly sound at start!

但你可以做一个小黑客,使用户不会此话,他已启动的声音为您的应用程序:

But you can make a little hack, so that the user will not remark that he currently started the audio for your application:


  1. 您将需要一个用户交互来开始你的声音。所以,你的应用程序或游戏,也许有一个启动画面还是一个值得欢迎的按钮,需要点击才能到的MainMenu或开始游戏。绑定到click事件,并调用load()方法,为你的<音频方式>

绑定到&LT的canplaythrough事件;音频> 。当你的来源是准备玩会触发此事件。在这里,您现在可以调用播放(),暂停()或等待其他交互。因此,音频已准备就绪,但现在你有充分的控制研究何时开始或停止声音。

Bind to the "canplaythrough" event of the <audio>. This event is triggered when your source is ready to play. Here you can now call play(), pause() or wait for other interactions. So the audio is ready but now you have full controll when to start or stop the sound.

我也建议你在移动客户端使用的音频精灵。的iOS(和Android?)内部通过一个Singleton实现音频支持。这意味着,你不能像在桌面浏览器,有10个音频元素和声音玩一次型动物。您只能播放一个文件!
所以改不同的声音源花费很多时间。与音频精灵你就可以开始你的精灵,当用户第一次与您的网站或游戏进行互动。暂停你的精灵,当你需要播放声音,你必须设置currentTime的以精灵的开始,当你的文件的currentTime的达到你的精灵月底暂停精灵。有一个timeupdate事件在您可以检查你的精灵的currentTime的。

I also advise you to use audio sprites on mobile clients. iOS (and Android?) internally implemented audio support through a Singleton. That means that you cannot, like in desktop browser, have 10 audio elements and play differents sound at once. You can only play one file! So changing the source for different sounds takes to much time. With an audio sprite you can start your sprites when the user first interact with your website or game. Pause your sprite and when you need to play sound you have to set the currentTime to the beginning of the sprite and pause the sprite when currentTime of your file reaches the end of your sprite. There is an timeupdate Event where your can check the currentTime of your sprite.

如果您更感兴趣,我可以prepare我的JavaScript的音频播放器精灵为你!

If you are more interested I can prepare my javascript audio sprites player for you!!

这篇关于在移动Safari浏览器自动播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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