简体   繁体   English

在iBooks中翻页时,Javascript会间歇性地触发

[英]Javascript firing intermittently when turning page in iBooks

I'm trying to add the ability to play audio in my ebook and for the user to be able to pause and start this if they want. 我正在尝试添加在我的电子书中播放音频的功能,并且用户可以根据需要暂停和启动它。

I can do this in javascript by controlling a standard HTML5 audio tag on a page but when the user turns the page I want the next page to remember the state ie if the audio is paused or not. 我可以通过控制页面上的标准HTML5音频标签在javascript中执行此操作,但是当用户翻页时,我希望下一页记住状态,即音频是否暂停。

The problem seems to be that when each page turns the javascript sometimes fires and sometimes doesn't. 问题似乎是当每个页面转动时,javascript有时会触发,有时则不会。 I've tried adding a standard listener 我试过添加一个标准的监听器

'document.addEventListener("DOMContentLoaded", function(event) { ` 'document.addEventListener(“DOMContentLoaded”,function(event){`

in the HEAD but this does not always fire when the page turns. 在HEAD中但是当页面转动时并不总是触发。 When I try it in Chrome it works perfectly. 当我在Chrome中试用它时效果很好。

Any help would be appreciated 任何帮助,将不胜感激

EDIT: here is the code I am trying to run: 编辑:这是我试图运行的代码:

document.addEventListener("DOMContentLoaded", function(event) { 
  do work
    if (document.getElementById("audioreadalong") && localStorage.playaudio == "1"){
        if (localStorage.isPaused == "0"){
            document.getElementById("audioreadalong").play();
            if (document.getElementById("play") && document.getElementById("pause")){
                document.getElementById("play").setAttribute("style", "display: none;");
                document.getElementById("pause").setAttribute("style", "display: block;");
            }
        }
        else if (localStorage.isPaused == "1"){
            document.getElementById("audioreadalong").pause();
            if (document.getElementById("play") && document.getElementById("pause")){
                document.getElementById("play").setAttribute("style", "display: block;");
                document.getElementById("pause").setAttribute("style", "display: none;");
            }
        }
    }
    else if(localStorage.playaudio == "0"){
        if (document.getElementById("play") && document.getElementById("pause")){
            document.getElementById("play").setAttribute("style", "display: none;");
            document.getElementById("pause").setAttribute("style", "display: none;");
        }
    }
});

This is referenced in the head of each page I need audio on (the odd numbered, right ones not the even numbered left ones - each double page is 2 separate web pages). 这在我需要音频的每个页面的头部引用(奇数,右边的,而不是偶数左边的 - 每个双页是2个单独的网页)。

This code does not run every time the page turns. 每次页面转动时,此代码都不会运行。 It does in Chrome as each page reloads each time. 它在Chrome中,因为每次重新加载每个页面。 I can't see anything in the epub3 spec that would explain this. 我无法在epub3规范中看到任何可以解释这一点的内容。

FINAL EDIT: I can only assume that either a) there is a bug in the way iBooks runs javascript on a page; 最终编辑:我只能假设a)iBooks在页面上运行javascript的方式存在错误; or b) I have something misconfigured. 或b)我有错误的配置。 I have worked a way around my problem by using Apple's built-in support for Read Along Books in their Asset Guide . 在他们的资产指南中使用Apple对Read Along Books的内置支持解决了我的问题。 I've downloaded the sample project from iTunes and am now using SMILs with the iBooks namespace and iBooks.js (found in the sample project). 我已经下载从iTunes样本项目,现在用SMILs与iBooks的命名空间和iBooks.js(在样本项目中找到)。 If anyone needs help with this post a question and reference it here. 如果有人需要这篇文章的帮助,请在此处提及。

Thanks to those who tried to help or upvoted this. 感谢那些试图帮助或赞成此事的人。

You will have to store the current state of the audio in Local Storage like this: 您必须将音频的当前状态存储在本地存储中,如下所示:

localStorage.setItem("audioState", "true");

And just retrieve value of localstorage using: 并使用以下方法检索localstorage的值:

localStorage.getItem("audioState");

And implement conditions accordingly... 并相应地实施条件......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM