1

So I have an audio element in my HTML:

    <audio id="success-sound" class="hidden">
        <source src="sound/success.mp3" type="audio/mpeg">
    </audio>

The sound is played on click / touch event. It works fine in Safari on my Mac, the music plays. I am creating an iOS app though with a web view where I ember my HTML5 app. When I try the app in iPad 6.1 simulator and debug the console, I can see this error:

INVALID_STATE_ERR: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.

This is the line that's failing:

        var successSound = document.getElementById("success-sound");
        successSound.currentTime = 0;
        successSound.play();

Any ideas?

Btw, I have another audio element with a soundtrack which works:

    function startSoundtrack() {
        soundtrack.play();
        soundtrack.addEventListener('ended', function () {
            var audio = this;
            setTimeout(function () {
                audio.currentTime = 0;
                audio.play();
            }, 5000);
        }, false);
    }
4

1 回答 1

0

在 HTML5 方面,iOS 似乎有一些限制。似乎很难拥有多个音频元素并在第一个音频元素在后台播放时播放第二个音频元素。

我已经通过使用 AVAudioPlayer 解决了这个问题,当我想播放声音时,我使用 JavaScript 通知 Web 视图的视图控制器。

于 2013-09-02T10:55:08.710 回答