You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NarrationManager.js 6.3KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * NarrationManager.js
  3. * Keynote HTML Player
  4. *
  5. * Created by Tungwei Cheng
  6. * Copyright (c) 2013 Apple Inc. All rights reserved.
  7. */
  8. var NarrationManager = Class.create({
  9. initialize: function(recording) {
  10. // recording movies in an array
  11. this.movieSegments = recording.movieSegments;
  12. // total time of this recording
  13. this.duration = recording.duration;
  14. // navigation, movie and pause in eventTracks array
  15. this.eventTracks = recording.eventTracks;
  16. // current navigation event index
  17. this.currentNavigationEventIndex = 0;
  18. // last scene index
  19. this.lastSceneIndex = 0;
  20. for (var i = 0, length = this.eventTracks.length; i < length; i++) {
  21. var eventTrack = this.eventTracks[i];
  22. if (eventTrack.type === "navigation") {
  23. this.navigationEvents = eventTrack.events;
  24. } else if (eventTrack.type === "movie") {
  25. this.movieEvents = eventTrack.events;
  26. } else if (eventTrack.type === "pause") {
  27. this.pauseEvents = eventTrack.events;
  28. }
  29. }
  30. },
  31. start: function() {
  32. // set up media resources
  33. var audio = new Audio();
  34. audio.src = "../" + this.movieSegments[0].url;
  35. // observe play event
  36. Event.observe(audio, "playing", this.handleAudioDidStart.bind(this));
  37. Event.observe(audio, "ended", this.handleAudioDidEnd.bind(this, 0));
  38. audio.play();
  39. },
  40. handleAudioDidStart: function() {
  41. // audio has started, now navigate to the first navigation event
  42. setTimeout(this.navigate(this.navigationEvents[0], true), 100);
  43. },
  44. handleAudioDidEnd: function(audioIndex) {
  45. var nextAudioIndex = audioIndex + 1;
  46. if (this.movieSegments[nextAudioIndex]) {
  47. var audio = new Audio();
  48. audio.src = "../" + this.movieSegments[nextAudioIndex].url;
  49. audio.play();
  50. Event.stopObserving(audio, "ended");
  51. Event.observe(audio, "ended", this.handleAudioDidEnd.bind(this, nextAudioIndex));
  52. }
  53. },
  54. navigate: function(event, startup) {
  55. var sceneIndex = this.sceneIndexFromNavigationEvent(event);
  56. if (event.animationPhase === "start") {
  57. // if event's slideIndex has been changed from lastScene's slide
  58. // check to see if this is our next scene to play
  59. var isNextScene = false;
  60. if (gShowController.script.loopSlideshow) {
  61. if (this.lastSceneIndex === gShowController.script.numScenes - 1) {
  62. if (sceneIndex === 0) {
  63. isNextScene = true;
  64. }
  65. }
  66. } else {
  67. if (this.lastSceneIndex + 1 === sceneIndex) {
  68. isNextScene = true;
  69. }
  70. }
  71. if (isNextScene) {
  72. if (gShowController.state === kShowControllerState_IdleAtInitialState) {
  73. gShowController.playCurrentScene();
  74. } else if (gShowController.state === kShowControllerState_IdleAtFinalState) {
  75. gShowController.jumpToScene(this.lastSceneIndex, true);
  76. }
  77. } else {
  78. // this is the slide we are jumping to
  79. var slideIndexToJump = gShowController.scriptManager.slideIndexFromSceneIndex(sceneIndex);
  80. var sceneIndexOfHyperlink = this.lastSceneIndex;
  81. // get hyperlink from slide, find the first occurrence of slideId in hyperlinks
  82. var hyperlinks = gShowController.script.events[sceneIndexOfHyperlink].hyperlinks;
  83. var hyperlink;
  84. var hyperlinkEvent;
  85. for (var i = 0, length = hyperlinks.length; i < length; i++) {
  86. hyperlink = hyperlinks[i];
  87. hyperlinkEvent = hyperlink.events[event.slide];
  88. if (hyperlinkEvent) {
  89. break;
  90. }
  91. }
  92. if (hyperlink) {
  93. // call jumpToHyperlinkSlide to play hyperlink transition
  94. gShowController.jumpToHyperlinkSlide(slideIndexToJump, hyperlink);
  95. } else {
  96. // if no hyperlink event is found for any reason, we still want to jump
  97. gShowController.jumpToScene(sceneIndex, false);
  98. }
  99. }
  100. } else if (event.animationPhase === "none" && startup == null) {
  101. gShowController.jumpToScene(sceneIndex, false);
  102. }
  103. // if there is any more event then set it to run next
  104. var nextEvent = this.navigationEvents[this.currentNavigationEventIndex + 1];
  105. if (nextEvent == null) {
  106. return;
  107. }
  108. // set timeout to navigate to next event
  109. var duration = nextEvent.startTime - event.startTime;
  110. setTimeout(this.navigate.bind(this, nextEvent), duration * 1000);
  111. this.lastSceneIndex = sceneIndex;
  112. this.currentNavigationEventIndex = this.currentNavigationEventIndex + 1;
  113. },
  114. handleCurrentSceneDidComplete: function(sceneIndexToJump) {
  115. // scene did complete, jump to next scene so we have more time to set it up
  116. gShowController.jumpToScene(sceneIndexToJump, false);
  117. },
  118. sceneIndexFromNavigationEvent: function(event) {
  119. // return sceneIndex from navigation event
  120. var slideId = event.slide;
  121. var slideList = gShowController.script.slideList;
  122. var newSlideIndex = -1;
  123. for (var i = 0, length = slideList.length; i < length; i++) {
  124. if (slideList[i] === slideId) {
  125. newSlideIndex = i;
  126. break;
  127. }
  128. }
  129. var sceneIndex = gShowController.scriptManager.sceneIndexFromSlideIndex(newSlideIndex);
  130. var newSceneIndex = event.eventIndex + sceneIndex;
  131. return newSceneIndex;
  132. },
  133. slideIndexFromSlideId: function(slideId) {
  134. var slideList = gShowController.slideList;
  135. var slideIndex = -1;
  136. for (var i = 0, length = slideList.length; i < length; i++) {
  137. if (slideList[i] === slideId) {
  138. slideIndex = i;
  139. break;
  140. }
  141. }
  142. return slideIndex;
  143. }
  144. });