Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 3.4KB

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