将声音加载到音频标签并稍后播放 [英] Load a sound to audio tag and play it later

查看:91
本文介绍了将声音加载到音频标签并稍后播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面需要在特定实例播放声音(音频标签或js Audio对象)。音频文件位置不在我的本地网络服务器中,它位于另一个访问过的服务器,如https://site.com/folder/file.wav



我需要在页面加载时访问和加载此文件,需要播放它而不会有任何延迟。添加我当前的代码



I have a page for which i need to play a sound (audio tag or js Audio object) at a specific instance. The audio file location is not in my local webserver, it's at another server accessed like 'https://site.com/folder/file.wav'

I need to access and load this file at page load and need to play it without having a delay. Adding my current code below

<script> 
 var player = new Audio();

 $(document).ready(function () {
   player.src = 'https://site.com/folder/file.wav';
 });

function play()
{
player.play();
}
</script>





我现在面临的问题是,如果我正在运行它来自带有WIFI的电脑,按时播放;但如果我从我的Android手机上运行它会在3-5秒后播放。



请指教。



Problem i'm facing now is, if i'm running it from a computer with WIFI, it plays ontime; but if i run that from my android mobile it plays after 3-5 seconds.

Please advise.

推荐答案

document )。ready( function (){
player.src = < span class =code-string>' https://site.com/folder/file.wav';
});

function play()
{
player.play();
}
< / script>
(document).ready(function () { player.src = 'https://site.com/folder/file.wav'; }); function play() { player.play(); } </script>





我现在面临的问题是,如果我正在运行它来自带有WIFI的电脑,按时播放;但如果我从我的Android手机上运行它会在3-5秒后播放。



请指教。



Problem i'm facing now is, if i'm running it from a computer with WIFI, it plays ontime; but if i run that from my android mobile it plays after 3-5 seconds.

Please advise.


那3- 5秒是移动设备必须从服务器下载音乐的时间。问题是HTML5音频/视频标签仅在下载页面和在DOM中创建标签后才开始下载目标文件。即使启用了自动播放功能,您也无法做任何事情......
That 3-5 seconds is the time the mobile device have to download your music from the server. The problem is that HTML5 audio/video tag starts to download it's target file only after the page downloaded and the tag created in the DOM. Even with autoplay on, you can't do nothing with that...


这篇关于将声音加载到音频标签并稍后播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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