在移动浏览器中,什么才算是js的用户交互?

时间:2018-06-26 13:48:45

标签: javascript html audio mobile-safari media

我正在尝试通过JavaScript和HTML Audio API预先加载音频文件。

iOS(也称为safari移动版)上的Safari仅允许.load()进行“用户交互”。现在我想知道那到底是什么意思。

例如:我触发了

的最高答案中描述的代码

Preload multiple audio files

var audioFiles = [
    "http://www.teanglann.ie/CanC/nua.mp3",
    "http://www.teanglann.ie/CanC/ag.mp3",
    "http://www.teanglann.ie/CanC/dul.mp3",
    "http://www.teanglann.ie/CanC/freisin.mp3"
];
    
function preloadAudio(url) {
    var audio = new Audio();
    // once this file loads, it will call loadedAudio()
    // the file will be kept by the browser as cache
    audio.addEventListener('canplaythrough', loadedAudio, false);
    audio.src = url;
}
    
var loaded = 0;
function loadedAudio() {
    // this will be called every time an audio file is loaded
    // we keep track of the loaded files vs the requested files
    loaded++;
    if (loaded == audioFiles.length){
    	// all have loaded
    	init();
    }
}
    
var player = document.getElementById('player');
function play(index) {
    player.src = audioFiles[index];
    player.play();
}
    
function init() {
    // do your stuff here, audio has been loaded
    // for example, play all files one after the other
    var i = 0;
    // once the player ends, play the next one
    player.onended = function() {
    	i++;
        if (i >= audioFiles.length) {
            // end 
            return;
        }
    	play(i);
    };
    // play the first file
    play(i);
}
    
// we start preloading all the audio files
for (var i in audioFiles) {
    preloadAudio(audioFiles[i]);
}
<audio id="player"></audio>

在按钮上单击,但仍然存在无法缓存文件的问题...

for循环是否中断用户交互?在不再将load()视为“用户交互”之前,我可以在后台执行多少个步骤/函数调用?

0 个答案:

没有答案