我已經寫了一些JavaScript,成功地在桌面上拉取和播放音頻文件。但是,我無法在移動設備上看到這一點 - 這真的是我的意圖。Javascript - 將音頻推送到移動設備(移動Safari和Chrome)
眼下桌面,我可以把和播放音頻與此javascript:
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', url);
audioElement.setAttribute('autoplay', 'autoplay');
$.get();
audioElement.addEventListener("load", function() {
audioElement.play();
}, true);
(其中URL是我的Web服務器上的某個位置)
我讀過關於移動音頻需要初始用戶動作來激活,所以然後我試圖使:
var audioElement = document.createElement('audio');
全局元素,然後在的document.ready部分,我激活音頻當用戶新聞報s'tool'button:
$(document).ready(function() {
$("#toolButton").click(function() {
// Need a 'click' event to turn on sound for mobile devices
if (audio_init === undefined) {
audioElement.play();
}
audio_init = 1;
但是,這並沒有成功。
然後,我嘗試網絡音頻,再次,這個代碼可以讓我在桌面瀏覽器中播放,但沒有對手機:
var webaudio_play = function(url) {
ctx = new webkitAudioContext();
var req = new XMLHttpRequest();
req.open("GET",url,true);
req.responseType = "arraybuffer";
req.onload = function() {
ctx.decodeAudioData(req.response, function(buffer) {
var src = ctx.createBufferSource();
src.buffer = buffer;
src.connect(ctx.destination);
//src.start();
src.noteOn(0);
});
};
req.send();
}
我真的不知道接下來要去哪裏。有沒有人成功地將音頻文件推送到移動設備?
那麼,你似乎已經嘗試了一切。任何錯誤?你有沒有檢查過代碼是否被執行?它只是默默地失敗了嗎? – Touffy
它只是失敗而已。我沒有連接平板電腦,所以我可以通過桌面Chrome或Safari瀏覽器進行調試。 – HP1