Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // @flow
  2. import type { AudioElement } from '../media';
  3. import {
  4. _ADD_AUDIO_ELEMENT,
  5. _REMOVE_AUDIO_ELEMENT,
  6. PLAY_SOUND,
  7. REGISTER_SOUND,
  8. STOP_SOUND,
  9. UNREGISTER_SOUND
  10. } from './actionTypes';
  11. import { getSoundsPath } from './functions';
  12. /**
  13. * Adds {@link AudioElement} instance to the base/sounds feature state for the
  14. * {@link Sound} instance identified by the given id. After this action the
  15. * sound can be played by dispatching the {@link PLAY_SOUND} action.
  16. *
  17. * @param {string} soundId - The sound identifier for which the audio element
  18. * will be stored.
  19. * @param {AudioElement} audioElement - The audio element which implements the
  20. * audio playback functionality and which is backed by the sound resource
  21. * corresponding to the {@link Sound} with the given id.
  22. * @protected
  23. * @returns {{
  24. * type: PLAY_SOUND,
  25. * audioElement: AudioElement,
  26. * soundId: string
  27. * }}
  28. */
  29. export function _addAudioElement(soundId: string, audioElement: AudioElement) {
  30. return {
  31. type: _ADD_AUDIO_ELEMENT,
  32. audioElement,
  33. soundId
  34. };
  35. }
  36. /**
  37. * The opposite of {@link _addAudioElement} which removes {@link AudioElement}
  38. * for given sound from base/sounds state. It means that the audio resource has
  39. * been disposed and the sound can no longer be played.
  40. *
  41. * @param {string} soundId - The {@link Sound} instance identifier for which the
  42. * audio element is being removed.
  43. * @protected
  44. * @returns {{
  45. * type: _REMOVE_AUDIO_ELEMENT,
  46. * soundId: string
  47. * }}
  48. */
  49. export function _removeAudioElement(soundId: string) {
  50. return {
  51. type: _REMOVE_AUDIO_ELEMENT,
  52. soundId
  53. };
  54. }
  55. /**
  56. * Starts playback of the sound identified by the given sound id. The action
  57. * will have effect only if the audio resource has been loaded already.
  58. *
  59. * @param {string} soundId - The id of the sound to be played (the same one
  60. * which was used in {@link registerSound} to register the sound).
  61. * @returns {{
  62. * type: PLAY_SOUND,
  63. * soundId: string
  64. * }}
  65. */
  66. export function playSound(soundId: string): Object {
  67. return {
  68. type: PLAY_SOUND,
  69. soundId
  70. };
  71. }
  72. /**
  73. * Registers a new sound for given id and a source object which can be either a
  74. * path or a raw object depending on the platform (native vs web). It will make
  75. * the {@link SoundCollection} render extra HTMLAudioElement which will make it
  76. * available for playback through the {@link playSound} action.
  77. *
  78. * @param {string} soundId - The global identifier which identify the sound
  79. * created for given source object.
  80. * @param {string} soundName - The name of bundled audio file that will be
  81. * associated with the given {@code soundId}.
  82. * @param {Object} options - Optional paramaters.
  83. * @param {boolean} options.loop - True in order to loop the sound.
  84. * @returns {{
  85. * type: REGISTER_SOUND,
  86. * soundId: string,
  87. * src: string,
  88. * options: {
  89. * loop: boolean
  90. * }
  91. * }}
  92. */
  93. export function registerSound(
  94. soundId: string, soundName: string, options: Object = {}): Object {
  95. return {
  96. type: REGISTER_SOUND,
  97. soundId,
  98. src: `${getSoundsPath()}/${soundName}`,
  99. options
  100. };
  101. }
  102. /**
  103. * Stops playback of the sound identified by the given sound id.
  104. *
  105. * @param {string} soundId - The id of the sound to be stopped (the same one
  106. * which was used in {@link registerSound} to register the sound).
  107. * @returns {{
  108. * type: STOP_SOUND,
  109. * soundId: string
  110. * }}
  111. */
  112. export function stopSound(soundId: string): Object {
  113. return {
  114. type: STOP_SOUND,
  115. soundId
  116. };
  117. }
  118. /**
  119. * Unregister the sound identified by the given id. It will make the
  120. * {@link SoundCollection} component stop rendering the corresponding
  121. * {@code HTMLAudioElement} which then should result in the audio resource
  122. * disposal.
  123. *
  124. * @param {string} soundId - The identifier of the {@link Sound} to be removed.
  125. * @returns {{
  126. * type: UNREGISTER_SOUND,
  127. * soundId: string
  128. * }}
  129. */
  130. export function unregisterSound(soundId: string): Object {
  131. return {
  132. type: UNREGISTER_SOUND,
  133. soundId
  134. };
  135. }