react_player.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """React-Player component."""
  2. from __future__ import annotations
  3. from typing import Any, TypedDict
  4. from reflex.components.component import NoSSRComponent
  5. from reflex.event import EventHandler, no_args_event_spec, passthrough_event_spec
  6. from reflex.vars.base import Var
  7. class Progress(TypedDict):
  8. """Callback containing played and loaded progress as a fraction, and playedSeconds and loadedSeconds in seconds."""
  9. played: float
  10. playedSeconds: float
  11. loaded: float
  12. loadedSeconds: float
  13. class ReactPlayer(NoSSRComponent):
  14. """Using react-player and not implement all props and callback yet.
  15. reference: https://github.com/cookpete/react-player.
  16. """
  17. library = "react-player@2.16.0"
  18. tag = "ReactPlayer"
  19. is_default = True
  20. # The url of a video or song to play
  21. url: Var[str]
  22. # Set to true or false to pause or play the media
  23. playing: Var[bool]
  24. # Set to true or false to loop the media
  25. loop: Var[bool]
  26. # Set to true or false to display native player controls.
  27. controls: Var[bool] = Var.create(True)
  28. # Set to true to show just the video thumbnail, which loads the full player on click
  29. light: Var[bool]
  30. # Set the volume of the player, between 0 and 1
  31. volume: Var[float]
  32. # Mutes the player
  33. muted: Var[bool]
  34. # Called when media is loaded and ready to play. If playing is set to true, media will play immediately.
  35. on_ready: EventHandler[no_args_event_spec]
  36. # Called when media starts playing.
  37. on_start: EventHandler[no_args_event_spec]
  38. # Called when media starts or resumes playing after pausing or buffering.
  39. on_play: EventHandler[no_args_event_spec]
  40. # Callback containing played and loaded progress as a fraction, and playedSeconds and loadedSeconds in seconds. eg { played: 0.12, playedSeconds: 11.3, loaded: 0.34, loadedSeconds: 16.7 }
  41. on_progress: EventHandler[passthrough_event_spec(Progress)]
  42. # Callback containing duration of the media, in seconds.
  43. on_duration: EventHandler[passthrough_event_spec(float)]
  44. # Called when media is paused.
  45. on_pause: EventHandler[no_args_event_spec]
  46. # Called when media starts buffering.
  47. on_buffer: EventHandler[no_args_event_spec]
  48. # Called when media has finished buffering. Works for files, YouTube and Facebook.
  49. on_buffer_end: EventHandler[no_args_event_spec]
  50. # Called when media seeks with seconds parameter.
  51. on_seek: EventHandler[passthrough_event_spec(float)]
  52. # Called when playback rate of the player changed. Only supported by YouTube, Vimeo (if enabled), Wistia, and file paths.
  53. on_playback_rate_change: EventHandler[no_args_event_spec]
  54. # Called when playback quality of the player changed. Only supported by YouTube (if enabled).
  55. on_playback_quality_change: EventHandler[no_args_event_spec]
  56. # Called when media finishes playing. Does not fire when loop is set to true.
  57. on_ended: EventHandler[no_args_event_spec]
  58. # Called when an error occurs whilst attempting to play media.
  59. on_error: EventHandler[no_args_event_spec]
  60. # Called when user clicks the light mode preview.
  61. on_click_preview: EventHandler[no_args_event_spec]
  62. # Called when picture-in-picture mode is enabled.
  63. on_enable_pip: EventHandler[no_args_event_spec]
  64. # Called when picture-in-picture mode is disabled.
  65. on_disable_pip: EventHandler[no_args_event_spec]
  66. def _render(self, props: dict[str, Any] | None = None):
  67. """Render the component. Adds width and height set to None because
  68. react-player will set them to some random value that overrides the
  69. css width and height.
  70. Args:
  71. props: The props to pass to the component.
  72. Returns:
  73. The rendered component.
  74. """
  75. return (
  76. super()
  77. ._render(props)
  78. .add_props(
  79. width=Var.create(None),
  80. height=Var.create(None),
  81. )
  82. )