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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. UNREGISTER_SOUND
  9. } from './actionTypes';
  10. /**
  11. * Adds {@link AudioElement} instance to the base/sounds feature state for the
  12. * {@link Sound} instance identified by the given id. After this action the
  13. * sound can be played by dispatching the {@link PLAY_SOUND} action.
  14. *
  15. * @param {string} soundId - The sound identifier for which the audio element
  16. * will be stored.
  17. * @param {AudioElement} audioElement - The audio element which implements the
  18. * audio playback functionality and which is backed by the sound resource
  19. * corresponding to the {@link Sound} with the given id.
  20. * @protected
  21. * @returns {{
  22. * type: PLAY_SOUND,
  23. * audioElement: AudioElement,
  24. * soundId: string
  25. * }}
  26. */
  27. export function _addAudioElement(soundId: string, audioElement: AudioElement) {
  28. return {
  29. type: _ADD_AUDIO_ELEMENT,
  30. audioElement,
  31. soundId
  32. };
  33. }
  34. /**
  35. * The opposite of {@link _addAudioElement} which removes {@link AudioElement}
  36. * for given sound from base/sounds state. It means that the audio resource has
  37. * been disposed and the sound can no longer be played.
  38. *
  39. * @param {string} soundId - The {@link Sound} instance identifier for which the
  40. * audio element is being removed.
  41. * @protected
  42. * @returns {{
  43. * type: _REMOVE_AUDIO_ELEMENT,
  44. * soundId: string
  45. * }}
  46. */
  47. export function _removeAudioElement(soundId: string) {
  48. return {
  49. type: _REMOVE_AUDIO_ELEMENT,
  50. soundId
  51. };
  52. }
  53. /**
  54. * Starts playback of the sound identified by the given sound id. The action
  55. * will have effect only if the audio resource has been loaded already.
  56. *
  57. * @param {string} soundId - The id of the sound to be played (the same one
  58. * which was used in {@link registerSound} to register the sound).
  59. * @returns {{
  60. * type: PLAY_SOUND,
  61. * soundId: string
  62. * }}
  63. */
  64. export function playSound(soundId: string): Object {
  65. return {
  66. type: PLAY_SOUND,
  67. soundId
  68. };
  69. }
  70. /**
  71. * Registers a new sound for given id and a source object which can be either a
  72. * path or a raw object depending on the platform (native vs web). It will make
  73. * the {@link SoundCollection} render extra HTMLAudioElement which will make it
  74. * available for playback through the {@link playSound} action.
  75. *
  76. * @param {string} soundId - The global identifier which identify the sound
  77. * created for given source object.
  78. * @param {Object|string} src - Either path to an audio file or a raw object
  79. * which specifies the audio resource that will be associated with the given
  80. * {@code soundId}.
  81. * @returns {{
  82. * type: REGISTER_SOUND,
  83. * soundId: string,
  84. * src: (Object | string)
  85. * }}
  86. */
  87. export function registerSound(soundId: string, src: Object | string): Object {
  88. return {
  89. type: REGISTER_SOUND,
  90. soundId,
  91. src
  92. };
  93. }
  94. /**
  95. * Unregister the sound identified by the given id. It will make the
  96. * {@link SoundCollection} component stop rendering the corresponding
  97. * {@code HTMLAudioElement} which then should result in the audio resource
  98. * disposal.
  99. *
  100. * @param {string} soundId - The identifier of the {@link Sound} to be removed.
  101. * @returns {{
  102. * type: UNREGISTER_SOUND,
  103. * soundId: string
  104. * }}
  105. */
  106. export function unregisterSound(soundId: string): Object {
  107. return {
  108. type: UNREGISTER_SOUND,
  109. soundId
  110. };
  111. }