Media Chrome

repository·main·Indexed 25 days ago

https://github.com/muxinc/media-chrome

A library of customizable native Web Components for building audio and video player controls. Compatible with any media element and various JavaScript frameworks, it provides elements like <media-controller>, <media-control-bar>, and <media-play-button> to create flexible media interfaces.

Tokens
48.5K
Snippets
156
Records
238
Agent score
82%

What's inside media-chrome

  1. Use the <media-cast-button> component

    main

    The <media-cast-button> component allows users to open the Cast menu to select a Chromecast-enabled device or stop casting once a session has started.

    Requirements:

    • Requires a <castable-video> element or a custom media element that implements the CastableMedia interface (e.g., <mux-video>).
    • Google Cast support is only available in Chromium-based browsers.

    Behavior:

    • When not casting, the enter slot is displayed.
    • When currently casting, the exit slot is displayed.
    <media-controller>
      <castable-video
        playsinline muted crossorigin
        slot="media"
        src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
      ></castable-video>
      <media-cast-button></media-cast-button>
    </media-controller>
  2. Customize <media-fullscreen-button> icons using slots

    main

    You can replace the default icons in <media-fullscreen-button> using specific slots:

    • enter slot: Content displayed when the media is not fullscreen.
    • exit slot: Content displayed when the media is fullscreen.
    • icon slot: A single element used to represent both states (useful for animated icons or single-element transitions).

    To use enter and exit slots, provide separate elements for each.

    <media-fullscreen-button>
      <span slot="enter">Enter</span>
      <span slot="exit">Exit</span>
    </media-fullscreen-button>
  3. Use the <media-play-button> component

    main

    The <media-play-button> component toggles playback of media content. It automatically updates its displayed content based on the media playback state.

    • When media is playing, it displays the contents of the pause slot.
    • When media is paused, it displays the contents of the play slot.
    <media-controller>
      <video
        slot="media"
        src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
        playsinline
        muted
      ></video>
      <media-play-button></media-play-button>
    </media-controller>
  4. Style <media-volume-range> using Media UI Attributes

    main

    While <media-volume-range> does not expose configuration attributes, it automatically applies Media UI Attributes when the volume state changes. You can use these attributes as CSS selectors to apply conditional styling.

    Common use cases:

    • Hiding the component when volume is unavailable using [mediavolumeunavailable].
    • Changing styles when the media is muted using [mediamuted].
    /* Hide the volume range if volume is unavailable */
    media-volume-range[mediavolumeunavailable] {
      display: none;
    }
    
    /* Set background to red if the media is muted */
    media-volume-range[mediamuted] {
      --media-control-background: red;
    }
  5. Migrate attributes from kebab-case to lowercase

    main

    In Media Chrome v1.0+, all attributes have been changed from kebab-case to lowercase (smushedcase). To migrate, remove the hyphens from your HTML attributes and CSS selectors.

    HTML Attributes

    Replace attributes like seek-offset with seekoffset and show-duration with showduration.

    CSS Selectors

    When using media state attributes for conditional styling, remove hyphens from the attribute names. For example, [media-airplay-unavailable] becomes [mediaairplayunavailable].

    Breakpoint Attributes

    Breakpoint attributes for responsive design are also lowercase. For example, [breakpoint-md] becomes [breakpointmd].

    <!-- Before v1.0 -->
    <media-seek-backward-button seek-offset="30"></media-seek-backward-button>
    <media-time-display show-duration remaining></media-time-display>
    
    <!-- After v1.0 -->
    <media-seek-backward-button seekoffset="30"></media-seek-backward-button>
    <media-time-display showduration remaining></media-time-display>
  6. Use the <media-pip-button> component

    main

    The <media-pip-button> component toggles picture-in-picture (PiP) mode for media. It automatically updates its displayed content based on the current PiP state.

    Note that PiP support varies by browser; if the browser does not support PiP, the button may not function.

    <media-controller defaultsubtitles>
      <video
        playsinline muted crossorigin
        slot="media"
        src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
      ></video>
      <media-pip-button></media-pip-button>
    </media-controller>
  7. Use `<media-control-bar>` for default control layouts

    main

    The <media-control-bar> component simplifies the layout and styling of media controls. When placed inside a <media-controller>, it automatically grows to the width of the controller and will attempt to scale down child Media Chrome components if space is limited.

    <media-controller>
      <video
        playsinline muted crossorigin
        slot="media"
        src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
      ></video>
      <media-control-bar>
        <media-play-button></media-play-button>
        <media-seek-backward-button></media-seek-backward-button>
        <media-seek-forward-button></media-seek-forward-button>
        <media-mute-button></media-mute-button>
        <media-volume-range></media-volume-range>
        <media-time-display></media-time-display>
        <media-captions-button></media-captions-button>
        <media-playback-rate-button></media-playback-rate-button>
        <media-pip-button></media-pip-button>
        <media-fullscreen-button></media-fullscreen-button>
        <media-airplay-button></media-airplay-button>
      </media-control-bar>
    </media-controller>