[英]User interaction needed to play audio workaround?
Currently I am working on a html5/js music player application. 目前,我正在开发html5 / js音乐播放器应用程序。 I have many users on mobile devices that have problems with playing the music.
我在移动设备上有很多用户在播放音乐时遇到问题。 According to many websites you need to have user interaction before you can play audio.
根据许多网站的介绍,您需要先与用户互动,然后才能播放音频。
Currently this is how I have programmed my music player. 目前,这就是我对音乐播放器进行编程的方式。 I have used the onclick attribute to detect taps/clicks on a play button.
我已经使用onclick属性来检测播放按钮上的点击/点击。
<button onclick = "playaudio('song name');">Play</button>
Then I have code for js to resolve a play url. 然后我有用于js的代码来解析播放网址。
var http = new XMLHttpRequest();
var url = '/getsong';
var params = 'name='+songname;
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
var snd=document.getElementsByTagName("audio")[0];
snd.src = http.responseText;
snd.load();
snd.play();
}
}
http.send(params);
This works fine on a computer but on mobile users have to click the pause/play button to start the music. 这在计算机上工作正常,但在移动用户上,必须单击“暂停/播放”按钮才能开始播放音乐。 Is there any workaround for the user interaction requirement, because I want to implement a remote play system like Google Cast/Spotify Connect.
用户交互需求是否有任何解决方法,因为我想实现一个远程播放系统,例如Google Cast / Spotify Connect。
Currently I don't have access to the complete code so this is just the core part of the music player. 目前,我无法访问完整的代码,因此这只是音乐播放器的核心部分。
Also I need to make a playlist feature but due to this, it seems quite impossible to play the next track in the playlist without user interaction. 另外,我还需要制作播放列表功能,因此,如果没有用户交互,播放播放列表中的下一首曲目似乎是不可能的。
Previously asked question said audio autoplay is not allowed on some mobile browser, you can implement, there are several workarounds. 前面的问题说,某些移动浏览器不允许音频自动播放,可以实施,有几种解决方法。
Audio Tag Autoplay Not working in mobile 音频标签自动播放在移动设备中不起作用
And yes, regarding the player, you can opt for an open source HTML5 Player, Amplitude.js which supports playlist as well as next song plays without user interaction based on playlist, you can check a demo on their website 是的,对于播放器,您可以选择一个开放源代码的HTML5播放器Amplitude.js,该播放器支持播放列表以及下一首歌曲,而无需用户基于播放列表进行交互,您可以在其网站上查看演示
https://521dimensions.com/open-source/amplitudejs https://521dimensions.com/open-source/amplitudejs
Refer to code on their GitHub: 请参阅其GitHub上的代码:
https://github.com/521dimensions/amplitudejs https://github.com/521dimensions/amplitudejs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.