if (video.canPlayType("application/vnd.apple.mpegurl")) { // Safari video.src = src; }…
- User
if (video.canPlayType("application/vnd.apple.mpegurl")) { // Safari video.src = src; }
how can i tell if a source is a regular video file (.mp4 for eg) and assign that src to video.src since hls.js won't be needed for that. nextjs app btw
- Assistant
In a Next.js app, you can determine whether a video source is a regular file (like
.mp4) or an HLS stream (.m3u8) by checking the file extension or MIME type. Based on that, you can decide whether to use the native HLS support (on Safari) or load it throughhls.js(for other browsers).Here’s how you can do it:
✅ Goal:
- If the source is a regular MP4 (or WebM/Ogg), assign directly to
video.src. - If it's an HLS stream (
.m3u8or appropriate MIME type), usehls.js.
✅ Solution
You can check the source URL for the file extension or MIME type like this:
const src = 'your-video-url-here.mp4'; // or .m3u8 const video = document.getElementById('myVideo'); if (Hls.isSupported()) { const hls = new Hls(); // Check if it's an HLS stream (by URL or MIME) if (src.endsWith('.m3u8') || src.includes('application/vnd.apple.mpegurl')) { hls.loadSource(src); hls.attachMedia(video); hls.on(Hls.Events.MANIFEST_PARSED, () => video.play()); } else { // It's a regular video file like .mp4 video.src = src; video.play(); } } else if (video.canPlayType('application/vnd.apple.mpegurl')) { // Safari supports HLS natively video.src = src; video.addEventListener('loadedmetadata', () => video.play()); }
🧠 Notes:
-
Check for
.m3u8in URL:src.endsWith('.m3u8')This is a simple and effective way to detect HLS streams.
-
Use MIME Type (Optional): If your backend provides MIME types, you can also check:
if (type === 'application/vnd.apple.mpegurl') { ... } -
Safari Native HLS Support: Safari doesn't need
hls.js, so we usevideo.canPlayType("application/vnd.apple.mpegurl")to detect that capability.
🔁 Optional: Helper Function
If you're doing this in multiple places, make a helper:
function isHlsStream(url) { return url.includes('.m3u8') || url.includes('application/vnd.apple.mpegurl'); }
🧪 Example with Next.js (Client Component)
'use client'; import { useEffect } from 'react'; import Hls from 'hls.js'; export default function VideoPlayer({ src }) { useEffect(() => { const video = document.getElementById('myVideo'); if (Hls.isSupported()) { const hls = new Hls(); if (src.endsWith('.m3u8')) { hls.loadSource(src); hls.attachMedia(video); hls.on(Hls.Events.MANIFEST_PARSED, () => video.play()); } else { video.src = src; video.play(); } } else if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = src; video.addEventListener('loadedmetadata', () => video.play()); } return () => { if (hls) { hls.destroy(); } }; }, [src]); return <video id="myVideo" controls autoPlay muted></video>; }
Let me know if you want SSR-safe loading or dynamic imports for
hls.js! - If the source is a regular MP4 (or WebM/Ogg), assign directly to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.