[英]HTML5 Audio Tag cannot raise event `canplay` on mobile
I'm custom an audio player with JS and HTML5. 我使用JS和HTML5自定义了音频播放器。 The first, I do simple with:
首先,我简单地做一下:
<div id="btnPlayPause" playing="" state=""></div>
<audio style="display:none;" id="realSounder" preload="meta">
<source src="" type="audio/mpeg">
</audio>
And JS: 和JS:
$("#btnPlayPause").click(function(){
realSounder = document.getElementById('realSounder');
realSounder.src = "http://domain.com/aloha.mp3";
realSounder.play();
});
It runs well. 运行良好。 But when I implement with UI control, need more complex code:
但是当我使用UI控件实现时,需要更复杂的代码:
function initPlayer () {
realSounder = document.getElementById('realSounder');
realSounder.volume = 0.8;
subscribe('canplay', realSounder, function() {
if (realSounder.getAttribute('state') != 'playing')
realSounder.play();
});
subscribe('canplaythrough', realSounder, function() {
if (realSounder.getAttribute('state') != 'playing')
realSounder.play();
});
}
function subscribe(event, element, func) {
//addEventListener for cross-browser.
if (element.addEventListener) {
element.addEventListener(event, func, false);
} else if (element.attachEvent) {
element.attachEvent("on" + event, func);
} else {
element['on' + event] = func;
}
}
$(document).ready(function() {
initPlayer();
$("#btnPlayPause").click(function(e) {
e.preventDefault();
//do something for display
play();
});
});
function play() {
var loadUrl = "/wap.php/AjaxLoadAudio";
var audioId = $("#btnPlayPause").attr('playing'); //get id of audio
$.getJSON(
loadUrl,
{audio_id:audioId},
function(json){
realSounder.src = json.audio_share_url;
}
).error(function() {
console.log('Error: 404');
});
}
It doesn't works. 它不起作用。 It's seem tobe
realSounder
cannot raise event canplay
or canplaythrough
to play sound. realSounder
似乎无法引发事件canplay
或canplaythrough
来播放声音。 Why? 为什么? I'm try to console.log() or alert() in handler function at
canplay
and canplaythrough
but nothing happend! 我尝试在
canplay
和canplaythrough
处理函数中使用console.log()或alert(),但是什么也没发生!
I have tested this code and success on: 我已经在以下方面测试了此代码并获得了成功 :
And Fail on: 并失败 :
Mobile devices require direct, physical, synchronous interaction from the user to play audio or video. 移动设备需要用户直接,物理, 同步的交互才能播放音频或视频。 You've avoided the common pitfall of trying to play audio on page load by requiring the user to actually click a button, but you've broken the synchronous rule by making an asynchronous request with
$.getJSON
. 通过要求用户实际单击一个按钮,您避免了尝试在页面加载时播放音频的常见陷阱,但是通过使用
$.getJSON
发出异步请求,打破了同步规则。 You have stumbled across one of the only exceptions to the " never use synchronous XHRs " rule. 您偶然发现了“ 从不使用同步XHR ”规则的唯一例外之一。 I have never recommended this before, but try replacing your
play()
function with this: 我以前从未推荐过此方法,但尝试用以下方法替换
play()
函数:
function play() {
var audioId = $("#btnPlayPause").attr('playing'); //get id of audio
$.ajax({
url:"/wap.php/AjaxLoadAudio"
data: { audio_id: audioId },
dataType: "json",
async: false,
success: function(response){
realSounder.src = response.audio_share_url;
},
error: function() {
console.log("Error");
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.