简体   繁体   English

需要用户交互才能播放音频解决方法?

[英]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.

相关问题 如何在没有用户交互的情况下下载和播放音频剪辑? - How can I download and play audio clips without user interaction? 如何知道浏览器是否准备好播放音频(用户交互、PWA 下载等) - How to know if browser is ready to play audio (user interaction, PWA downloaded etc) iOS Safari 在没有用户交互的情况下播放声音 - iOS Safari play sound without user interaction 我可以在移动设备上一页上的用户交互中自动播放音频吗? - Can I autoplay audio from a user interaction on a previous page on mobile? HTML,JS:加载音频是否需要用户在移动浏览器上进行交互? - HTML, JS: Does loading audio need user interaction on mobile browsers? 检测是否<audio>由用户交互或缓冲区不足触发的“暂停”事件? - Detect if <audio> `pause` event fired by user interaction or buffer underrun? 使用 Mocha 在浏览器中测试音频(需要用户交互) - Testing audio in browser using Mocha (needs user interaction) 在 iOS 上一个接一个播放视频,无需用户交互 - Play videos one after another on iOS without user interaction 用户退出我的网站时播放音频 - Play audio when user exits my website 用户播放下一个音频时如何停止播放音频? - How to stop audio to play when user plays next audio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM