首页 > 解决方案 > Javascript - 音频元素不在 iOS 上播放

问题描述

每当我翻书时,我都会尝试播放音频元素。音频元素仅在书的第一页播放,但是当我翻页并触发功能bookTurned()时,音频不会在其他翻页上播放。此问题仅在 iOS 上发生,因为在每个其他 Web 浏览器上都会相应地播放音频。我已经看到这在 iOS 上是一个很常见的问题,但我无法弄清楚我需要做什么才能绕过这个问题并使其正常工作。有人可以解释一下为了在 iOS 上进行这项工作需要做什么吗?

编辑:在 iOS 上,我收到以下错误: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

async bookTurned(event, page, pageObject) {
        // Set first page turn
        if (!bookTimer) {
            bookTimer = new Timer();
        }

        // Determine direction
        const direction = page > prevPage ? DIRECTION.RIGHT : DIRECTION.LEFT;

        // Update turn index
        currTurnIndex = convertPageNumberToTurnIdx(page);

        // Play audio corresponding to the two pages we just turned to.
        await BOOK_AUDIO_PLAYER.play(currTurnIndex + 1);

        // Recalc max pages since turn js dynamically renders pages
        const currMaxPages = calcMaxPages();
        if (currMaxPages > maxPages) {
            maxPages = currMaxPages;
        }
}

BOOK_AUDIO_PLAYER处理音频

标签: javascriptaudiohtml5-audio

解决方案


推荐阅读