AudioElement.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. extendPrototype,
  3. } from '../utils/functionExtensions';
  4. import PropertyFactory from '../utils/PropertyFactory';
  5. import RenderableElement from './helpers/RenderableElement';
  6. import BaseElement from './BaseElement';
  7. import FrameElement from './helpers/FrameElement';
  8. function AudioElement(data, globalData, comp) {
  9. this.initFrame();
  10. this.initRenderable();
  11. this.assetData = globalData.getAssetData(data.refId);
  12. this.initBaseData(data, globalData, comp);
  13. this._isPlaying = false;
  14. this._canPlay = false;
  15. var assetPath = this.globalData.getAssetsPath(this.assetData);
  16. this.audio = this.globalData.audioController.createAudio(assetPath);
  17. this._currentTime = 0;
  18. this.globalData.audioController.addAudio(this);
  19. this._volumeMultiplier = 1;
  20. this._volume = 1;
  21. this._previousVolume = null;
  22. this.tm = data.tm ? PropertyFactory.getProp(this, data.tm, 0, globalData.frameRate, this) : { _placeholder: true };
  23. this.lv = PropertyFactory.getProp(this, data.au && data.au.lv ? data.au.lv : { k: [100] }, 1, 0.01, this);
  24. }
  25. AudioElement.prototype.prepareFrame = function (num) {
  26. this.prepareRenderableFrame(num, true);
  27. this.prepareProperties(num, true);
  28. if (!this.tm._placeholder) {
  29. var timeRemapped = this.tm.v;
  30. this._currentTime = timeRemapped;
  31. } else {
  32. this._currentTime = num / this.data.sr;
  33. }
  34. this._volume = this.lv.v[0];
  35. var totalVolume = this._volume * this._volumeMultiplier;
  36. if (this._previousVolume !== totalVolume) {
  37. this._previousVolume = totalVolume;
  38. this.audio.volume(totalVolume);
  39. }
  40. };
  41. extendPrototype([RenderableElement, BaseElement, FrameElement], AudioElement);
  42. AudioElement.prototype.renderFrame = function () {
  43. if (this.isInRange && this._canPlay) {
  44. if (!this._isPlaying) {
  45. this.audio.play();
  46. this.audio.seek(this._currentTime / this.globalData.frameRate);
  47. this._isPlaying = true;
  48. } else if (!this.audio.playing()
  49. || Math.abs(this._currentTime / this.globalData.frameRate - this.audio.seek()) > 0.1
  50. ) {
  51. this.audio.seek(this._currentTime / this.globalData.frameRate);
  52. }
  53. }
  54. };
  55. AudioElement.prototype.show = function () {
  56. // this.audio.play()
  57. };
  58. AudioElement.prototype.hide = function () {
  59. this.audio.pause();
  60. this._isPlaying = false;
  61. };
  62. AudioElement.prototype.pause = function () {
  63. this.audio.pause();
  64. this._isPlaying = false;
  65. this._canPlay = false;
  66. };
  67. AudioElement.prototype.resume = function () {
  68. this._canPlay = true;
  69. };
  70. AudioElement.prototype.setRate = function (rateValue) {
  71. this.audio.rate(rateValue);
  72. };
  73. AudioElement.prototype.volume = function (volumeValue) {
  74. this._volumeMultiplier = volumeValue;
  75. this._previousVolume = volumeValue * this._volume;
  76. this.audio.volume(this._previousVolume);
  77. };
  78. AudioElement.prototype.getBaseElement = function () {
  79. return null;
  80. };
  81. AudioElement.prototype.destroy = function () {
  82. };
  83. AudioElement.prototype.sourceRectAtTime = function () {
  84. };
  85. AudioElement.prototype.initExpressions = function () {
  86. };
  87. export default AudioElement;