not really known
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.

234 lines
5.2 KiB

  1. // TamTam audio stuff
  2. // Basic HTML 5 Audio Element encapsulation
  3. enyo.kind({
  4. name: "HTML5.Audio",
  5. kind: enyo.Control,
  6. tag: "audio",
  7. published: {
  8. src: "", crossorigin: "", preload: "auto",
  9. mediagroup: "", loop: false, muted: "", controlsbar: false
  10. },
  11. events: {
  12. onSoundEnded: ""
  13. },
  14. // Constructor
  15. create: function() {
  16. this.inherited(arguments);
  17. this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
  18. this.startPlay = false;
  19. this.srcChanged();
  20. this.crossoriginChanged();
  21. this.preloadChanged();
  22. this.loopChanged();
  23. this.mutedChanged();
  24. this.controlsbarChanged();
  25. this.handleVolumeButtons();
  26. },
  27. // Handle volume buttons on Android
  28. handleVolumeButtons: function() {
  29. if (this.isCordova && !enyo.platform.iOS) {
  30. // HACK: Need only on Android because Cordova intercept volume buttons
  31. var emptyf = function() {};
  32. document.addEventListener("volumeupbutton", function() {
  33. cordova.plugins.VolumeControl.getVolume(function(value) {
  34. var volume = parseInt(value);
  35. if (volume < 100) {
  36. cordova.plugins.VolumeControl.setVolume((volume+10), emptyf, emptyf);
  37. }
  38. }, emptyf);
  39. }, false);
  40. document.addEventListener("volumedownbutton", function() {
  41. cordova.plugins.VolumeControl.getVolume(function(value) {
  42. var volume = parseInt(value);
  43. if (volume > 0) {
  44. cordova.plugins.VolumeControl.setVolume((volume-1), emptyf, emptyf);
  45. }
  46. }, emptyf);
  47. }, false);
  48. }
  49. },
  50. // Render
  51. rendered: function() {
  52. this.inherited(arguments);
  53. // Handle init
  54. if (this.hasNode()) {
  55. // Handle sound ended event
  56. var audio = this;
  57. enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
  58. audio.doSoundEnded();
  59. });
  60. }
  61. },
  62. // Property changed
  63. srcChanged: function() {
  64. this.setAttribute("src", this.src);
  65. },
  66. crossoriginChanged: function() {
  67. this.setAttribute("crossorigin", this.crossorigin);
  68. },
  69. preloadChanged: function() {
  70. this.setAttribute("preload", this.preload);
  71. },
  72. loopChanged: function() {
  73. this.setAttribute("loop", this.loop);
  74. },
  75. mutedChanged: function() {
  76. if (this.muted.length != 0)
  77. this.setAttribute("muted", this.muted);
  78. },
  79. controlsbarChanged: function() {
  80. this.setAttribute("controls", this.controlsbar);
  81. },
  82. // Test if component could play a file type
  83. canPlayType: function(typename) {
  84. var node = this.hasNode();
  85. if (!node)
  86. return false;
  87. return node.canPlayType(typename);
  88. },
  89. // Play audio
  90. play: function() {
  91. // HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
  92. if (this.isCordova) {
  93. // Compute full path
  94. var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
  95. var that = this;
  96. if (this.media) {
  97. this.media.src = "";
  98. this.media.pause();
  99. this.media.release();
  100. }
  101. // Create the Media object
  102. this.media = new Media(src, function() { }, function() { },
  103. function(status) {
  104. if (status == 4 && this.src != "") {
  105. that.doSoundEnded();
  106. }
  107. }
  108. );
  109. // Play
  110. this.media.play();
  111. return;
  112. }
  113. var node = this.hasNode();
  114. if (!node)
  115. return;
  116. this.started = false;
  117. var promise = node.play();
  118. if (promise) {
  119. var that = this;
  120. promise.then(function() {
  121. that.started = true;
  122. }).catch(function() {});
  123. } else {
  124. this.started = true;
  125. }
  126. },
  127. // Pause audio
  128. pause: function() {
  129. // HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
  130. if (this.isCordova) {
  131. if (!this.media)
  132. return;
  133. this.media.src = "";
  134. this.media.pause();
  135. this.media.release();
  136. return;
  137. }
  138. var node = this.hasNode();
  139. if (!node)
  140. return;
  141. if (this.started) {
  142. node.pause();
  143. }
  144. },
  145. // Test if audio is paused
  146. paused: function() {
  147. var node = this.hasNode();
  148. if (!node)
  149. return false;
  150. return node.paused;
  151. },
  152. // Test if audio is ended
  153. ended: function() {
  154. var node = this.hasNode();
  155. if (!node)
  156. return false;
  157. return node.ended;
  158. }
  159. });
  160. // TamTam Audio engine
  161. enyo.kind({
  162. name: "TamTam.Audio",
  163. kind: enyo.Control,
  164. components: [
  165. { name: "sound", kind: "HTML5.Audio", preload: "auto", autobuffer: true, controlsbar: false,
  166. onSoundEnded: "broadcastEnd" }
  167. ],
  168. // Constructor
  169. create: function() {
  170. this.inherited(arguments);
  171. this.format = null;
  172. this.soundObject = null;
  173. },
  174. // First render, test sound format supported
  175. rendered: function() {
  176. this.inherited(arguments);
  177. if (this.$.sound.canPlayType("audio/ogg"))
  178. this.format = ".ogg";
  179. else if (this.$.sound.canPlayType("audio/mpeg"))
  180. this.format = ".mp3";
  181. },
  182. // Play a sound
  183. play: function(soundObject, loop) {
  184. if (this.soundObject) {
  185. this.soundObject.abort();
  186. }
  187. this.soundObject = soundObject;
  188. if (this.format == null)
  189. return;
  190. this.$.sound.setSrc(this.soundObject.sound+this.format);
  191. this.$.sound.setLoop(loop === true);
  192. this.timeStamp = new Date().getTime();
  193. this.render();
  194. this.$.sound.play();
  195. },
  196. // Pause
  197. pause: function() {
  198. if (this.format == null)
  199. return;
  200. this.$.sound.pause();
  201. },
  202. // End of sound detected, broadcast the signal
  203. broadcastEnd: function() {
  204. if (this.soundObject && this.soundObject.endofsound)
  205. this.soundObject.endofsound();
  206. this.soundObject = null;
  207. }
  208. });