js / html5 audio: Why is canplaythrough not fired on iOS safari?

前端 未结 3 1955
广开言路
广开言路 2021-01-02 05:01

i use the code below to preload an array of audio files (after user interacts with a button starting the process). After all audio files fired \"canplaythrough\" the code pr

3条回答
  •  太阳男子
    2021-01-02 05:23

    Try calling the load() method after setting the src.

    function preloadAudio(url)
    {
      console.log("trying to preload "+ 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.addEventListener('error', function failed(e)
      {
        console.log("COULD NOT LOAD AUDIO");
        $("#NETWORKERROR").show();
      });
      audio.src = url;
    
      audio.load();  // add this line
    }
    

提交回复
热议问题