Last active
December 17, 2021 17:17
-
-
Save dylanjha/288ff24af1ba30e4234b76a50d581be4 to your computer and use it in GitHub Desktop.
React airplay control for media-chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AirPlayButton from './airplay-button'; | |
function Player () { | |
const [hasAirplay, setHasAirplay] = useState(false); | |
const muxVideoRef = useRef(null); | |
useEffect(() => { | |
const onPlaybackTargetChanged = (event) => { | |
setHasAirplay(!!event.availability); | |
}; | |
if (window.WebKitPlaybackTargetAvailabilityEvent) { | |
// muxVideoRef is a ref to the `<mux-video>` DOM element | |
muxVideoRef.current.addEventListener('webkitplaybacktargetavailabilitychanged', onPlaybackTargetChanged); | |
} | |
}, []) | |
return ( | |
<media-controller> | |
<mux-video ref={muxVideoRef} /> | |
{/* all your other controls */} | |
{hasAirplay && <AirPlayButton onClick={() => muxVideoRef.current.webkitShowPlaybackTargetPicker()} />} | |
</media-controller> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment