我有一个非常简单的声音管理器2正常运行的示例,它不会在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:加载(intro-with-drums.mp3)soundmanager2.js:1190
sound0:loadstart soundmanager2.js:1190
sound0:暂停
声音0:加载的元数据soundmanager2.js:1190
sound0:loadeddata soundmanager2.js:1190
sound0:可以播放soundmanager2.js:1190
sound0:onload()soundmanager2.js:1188
声音0已加载

而iOS只做:

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

如何在iOS上使用此功能?

最佳答案

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

关于javascript - soundmanager2 html5音频无法在iOS上播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17868684/

10-09 14:03