在iOS中的Ajax响应中播放HTML5音频 [英] Playing html5 audio in ajax response in iOS

查看:99
本文介绍了在iOS中的Ajax响应中播放HTML5音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的搜索和研究,我发现iOS阻止了html5音频和视频的自动播放.音频和视频只能作为对单击事件的响应来播放.

After a lot of googling and researching, I have found out that iOS blocks autoplaying of html5 audio and video. Audio and Video can only be played as a response to a click event.

现在,我还有另一个问题.我经营一个音乐网站,其中包含许多用户生成的播放列表.单击播放"后,我发出ajax请求以获取曲目URL,然后加载并播放它们. iOS不会将其视为播放音乐的同步点击事件,因此在单击播放"按钮时音乐不会开始播放.我必须单击播放器控件的暂停和播放按钮以使其开始播放(基本上只使用pause()和play()).

Now, I have another problem. I run a music website with a lot of user generated playlists. After clicking play, I make an ajax request to fetch the track URLs, and then load them and play them. iOS doesn't consider this as a synchronous click event to play music, so the music doesn't start playing on clicking the play button. I have to click on the player control's pause and play buttons to make it start playing (which basically just use pause() and play()).

我只是想不起来该怎么办.在单击事件之前预先加载所有MP3 URL是不可行的.还有其他方法可以做到这一点吗?

I just can't think of what to do. Loading all the MP3 URLs before hand before the click event is not feasible. Is there any other way to get this done?

推荐答案

我知道这是一个古老的问题,但是对于遇到此问题的人们来说,有一个简单的解决方案,即直接从点击处理程序中播放简短的静音.理想情况下,这将是您已经获取并缓存的声音文件.

I know this is an ancient question, but for people coming across this there is a straightforward solution which is to play a short, silent sound directly from the click handler. Ideally, this would be a sound file you have already fetched and cached.

在那之后,您将能够从其他处理程序发起回放,即使没有直接用户交互的处理程序也是如此. (例如您的AJAX/XHR响应)

After that you'll be able to initiate playback from other handlers, even those without direct user interaction. (such as your AJAX/XHR response)

这篇关于在iOS中的Ajax响应中播放HTML5音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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