No sound on iOS 6 Web Audio API

后端 未结 12 1348
灰色年华
灰色年华 2020-12-02 09:41

I was really excited to see iOS 6 supports the Web Audio API, since we make HTML5 games. However, I cannot get iOS 6 to play any sound at all using the Web Audio API with e

12条回答
  •  抹茶落季
    2020-12-02 10:21

    I have come across the audio restrictions with HTML5 Audio on iOS and worked around the problem by:

    1) Creating an Audio Element with a silent audio file and playing it initially with a touch event (e.g. 'begin game' button) and then immidietly pausing it.

    2) Building a sound-switcher function which switches the Audio src and then plays the Audio element after a short timeout.

    3) Calling the sound-switcher function on any events (doesn't need to be a touch event).

    This works because the Audio Element is un-muted on first touch, with the silent audio file, and remains un-muted, so the source can be switched on-the-fly.

    switchSound: (id) ->
            @soundSwitch.pause()
            @soundSwitch.src = @sounds[id]._src
    
            clearTimeout @switchSoundDelay
            @switchSoundDelay = setTimeout =>
                # @soundSwitch.volume = 1
                @soundSwitch.play()
            ,50 
    

提交回复
热议问题