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.

actions.js 4.3KB

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