2012-09-18 44 views
0

我正在使用iOS6中的WebAudio API執行一些測試。看起來,當你點擊一個按鈕時,AudioBufferSourceNode將被正確創建並連接到上下文目標。在iOS 6中自動播放WebAudio API

我只是想知道是否有任何加載頁面時自動播放聲音的方式,因爲iOS6設備似乎正確構建SourceNode,但沒有聲音出現。

我已張貼下面我的代碼:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>HTML 5 WebAudio API Tests</title> 

    <script type="text/javascript"> 
     var audioContext, 
      audioBuffer; 

     function init() { 
      try { 
       audioContext = new webkitAudioContext(); 
       initBuffer('resources/dogBarking.mp3'); 
      } catch(e) { 
       console.error('webkitAudioContext is not supported in this browser'); 
      } 
     } 

     function initBuffer(desiredUrl) { 
      var url = desiredUrl || ''; 
      var request = new XMLHttpRequest(); 
      request.open("GET", url, true); 
      request.responseType = "arraybuffer"; 
      request.onload = function() { 
       audioContext.decodeAudioData(request.response, function(buffer) { 
        audioBuffer = buffer; 
       }, onError); 
      } 

      request.send(); 
     } 

     function playSound(startTime, endTime) { 
      var audioSource = audioContext.createBufferSource(); 
      audioSource.buffer = audioBuffer; 
      audioSource.connect(audioContext.destination); 
      audioSource.noteOn(0); 
      audioSource.noteOff(10); 
     } 
    </script> 
</head> 

<body onload="init()"> 
    <script type="text/javascript"> 
     (function() { 
      var buttonNode = document.createElement('input'); 
      buttonNode.setAttribute('type', 'button'); 
      buttonNode.setAttribute('name', 'playButton'); 
      buttonNode.setAttribute('onclick', 'playSound(0, 1)') 
      buttonNode.setAttribute('value', 'playSound()'); 
      document.body.appendChild(buttonNode); 

      document.write('<a id="test" onclick="playSound(0, 2)">hello</a>'); 

      var testLink = document.getElementById('test'); 
      var dispatch = document.createEvent('HTMLEvents'); 
      dispatch.initEvent("click", true, true); 
      testLink.dispatchEvent(dispatch); 
     }()); 
    </script> 
</body> 
</html> 

我希望這是有道理的。 親切的問候, 傑米。

回答

0

此問題與No sound on iOS 6 Web Audio API非常相似。 WebAudio API需要初始化用戶輸入,並且在以這種方式播放第一個聲音之後,WebAudioAPI將極大地工作,而無需在當前會話期間進行任何操作。

w.addEventListener('touchstart', myFunctionThatPlaysAnyWebAudioSound); 

您可以通過添加監聽器第一個窗口touchstart事件,像這樣的初始化API