soundmanager2 html5音频不会在ios上播放

时间:2013-07-25 21:02:40

标签: javascript jquery html5 audio soundmanager2

我有一个非常简单的soundmanager2启动和运行示例,它不会在IOS中触发:

    $(document).ready(function(){

    var introSound;

    setupSoundManager();

    function setupSoundManager(){
      soundManager.setup({

        // where to find the SWF files, if needed
        url: 'swf/',
        preferFlash: false,
        onready: function() {
          // SM2 has loaded, API ready to use e.g., createSound() etc.
          createSound();
        },

        ontimeout: function() {
          // Uh-oh. SWF missing, Flash blocked or other issue
          console.log('cant setup');
        }

      });
    }

    function createSound(){
      introSound = soundManager.createSound({
       url: 'intro-with-drums.mp3',
       // callback for when the sound has loaded
       onload: function() {
         soundManager._writeDebug(this.id + ' loaded');
         enablePlay(this);
       }
      }).load();
    }


    function enablePlay(thisSound){
      $('#sound').on('click',function(){
        introSound.play();
      });
    }



  });

桌面在加载时会经历这个阶段:

sound0:使用HTML5 soundmanager2.js:1190 sound0:load(intro-with-drums.mp3)soundmanager2.js:1190 sound0:loadstart soundmanager2.js:1190 sound0:暂停 sound0:loadedmetadata soundmanager2.js:1190 sound0:loadeddata soundmanager2.js:1190 sound0:canplay soundmanager2.js:1190 sound0:onload()soundmanager2.js:1188 sound0加载

和iOS只会:

sound0:使用HTML5 soundmanager2.js:1190 sound0:load(intro-with-drums.mp3)soundmanager2.js:1190 sound0:loadstart soundmanager2.js:1190 sound0:暂停

如何在iOS上使用它?

1 个答案:

答案 0 :(得分:1)

大多数移动浏览器在没有直接的物理交互的情况下会拒绝加载/缓冲任何音频/视频。这意味着您无法在页面加载或任何异步函数(如setTimeout)中触发初始播放/预加载。由于此限制,introSound永远不会加载,这意味着永远不会调用enablePlay,这意味着您的#sound元素永远不会将事件侦听器添加到其中。