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.

137 lines
2.9 KiB

  1. 
  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. onSoundTimeupdate: ""
  14. },
  15. // Constructor
  16. create: function() {
  17. this.inherited(arguments);
  18. this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
  19. this.startPlay = false;
  20. this.srcChanged();
  21. this.crossoriginChanged();
  22. this.preloadChanged();
  23. this.loopChanged();
  24. this.mutedChanged();
  25. this.controlsbarChanged();
  26. },
  27. // Render
  28. rendered: function() {
  29. this.inherited(arguments);
  30. // Handle init
  31. if (this.hasNode()) {
  32. // Handle sound ended event
  33. var audio = this;
  34. enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
  35. audio.doSoundEnded();
  36. });
  37. enyo.dispatcher.listen(audio.hasNode(), "timeupdate", function(s) {
  38. audio.doSoundTimeupdate({timeStamp: s.timeStamp});
  39. });
  40. }
  41. },
  42. // Property changed
  43. srcChanged: function() {
  44. this.setAttribute("src", this.src);
  45. },
  46. crossoriginChanged: function() {
  47. this.setAttribute("crossorigin", this.crossorigin);
  48. },
  49. preloadChanged: function() {
  50. this.setAttribute("preload", this.preload);
  51. },
  52. loopChanged: function() {
  53. this.setAttribute("loop", this.loop);
  54. },
  55. mutedChanged: function() {
  56. if (this.muted.length != 0)
  57. this.setAttribute("muted", this.muted);
  58. },
  59. controlsbarChanged: function() {
  60. this.setAttribute("controls", this.controlsbar);
  61. },
  62. // Test if component could play a file type
  63. canPlayType: function(typename) {
  64. var node = this.hasNode();
  65. if (!node)
  66. return false;
  67. return node.canPlayType(typename);
  68. },
  69. // Play audio
  70. play: function() {
  71. // HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
  72. if (this.isCordova) {
  73. var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
  74. var that = this;
  75. var media = new Media(src, function() {
  76. var timer = window.setInterval(function() {
  77. window.clearInterval(timer);
  78. that.doSoundEnded();
  79. }, this.getDuration()*1000);
  80. }, function() {});
  81. media.play();
  82. return;
  83. }
  84. var node = this.hasNode();
  85. if (!node)
  86. return;
  87. this.started = false;
  88. var promise = node.play();
  89. if (promise) {
  90. var that = this;
  91. promise.then(function() {
  92. that.started = true;
  93. }).catch(function() {});
  94. } else {
  95. this.started = true;
  96. }
  97. },
  98. // Pause audio
  99. pause: function() {
  100. var node = this.hasNode();
  101. if (!node)
  102. return;
  103. if (this.started) {
  104. node.pause();
  105. }
  106. },
  107. // Test if audio is paused
  108. paused: function() {
  109. var node = this.hasNode();
  110. if (!node)
  111. return false;
  112. return node.paused;
  113. },
  114. // Test if audio is ended
  115. ended: function() {
  116. var node = this.hasNode();
  117. if (!node)
  118. return false;
  119. return node.ended;
  120. }
  121. });