我有一个页面需要在特定实例播放声音(音频标签或js Audio对象)。音频文件位置不在我的本地网络服务器中,而是在其他服务器上访问,例如' https://site.com/folder/file.wav'
我需要在页面加载时访问和加载此文件,并且需要在没有延迟的情况下播放它。添加我当前的代码
<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秒后播放。
请告知。
答案 0 :(得分:0)
我认为您需要推迟显示页面,直到加载足够的音频以允许其播放(请参阅this answer)。
以下内容应足以让您入门:
// Hide the page until the audio has loaded.
// You might want to display some kind of preloader graphic
// while the user waits.
var $body = $('body').css('display', 'none');
$(document).ready(function () {
var player = new Audio();
// canplaythrough event is fired when enough of the audio has downloaded
// to play it through at the current download rate
player.addEventListener('canplaythrough', audioLoadedHandler);
function audioLoadedHandler(e) {
// Audio has loaded, show the page
$body.css('display', 'block');
// And start the audio
player.play();
}
player.src = 'https://site.com/folder/file.wav';
});