I have a Next. js app, and I want to embed YouTube placeholder so that I could play video in the chatbot itself. What should I do? Also, my front end is divided in two parts. Uh, one is web application, which is supported by Next. js, and other one is mobile application, which is built on React Native. Also, not just YouTube video. Uh, I want to support any kind of video. But first, let's try to embed YouTube videos.
embed YouTube video Next.js chatbot react-player 2025
react-native-youtube-iframe embed video chatbot React Native 2025
react-playerreact-player is the best single choice because:.mp4/.webm, and more - all with the same APInpm install react-player
VideoPlayer componentreact-player uses browser APIs, it must be client-side only in Next.js:// components/VideoPlayer.tsx
'use client';
import dynamic from 'next/dynamic';
const ReactPlayer = dynamic(() => import('react-player/lazy'), {
ssr: false,
loading: () => (
<div className="video-placeholder">Loading player...</div>
),
});
interface VideoPlayerProps {
url: string;
}
export default function VideoPlayer({ url }: VideoPlayerProps) {
return (
<div className="video-wrapper" style={{ position: 'relative', paddingTop: '56.25%' }}>
<ReactPlayer
url={url}
controls
width="100%"
height="100%"
style={{ position: 'absolute', top: 0, left: 0 }}
config={{
youtube: {
playerVars: { showinfo: 1 },
},
}}
/>
</div>
);
}
paddingTop: 56.25% trick maintains a 16:9 aspect ratio automatically.// components/ChatMessage.tsx
'use client';
import VideoPlayer from './VideoPlayer';
function isVideoUrl(text: string): boolean {
return (
text.startsWith('https://www.youtube.com/') ||
text.startsWith('https://youtu.be/') ||
text.startsWith('https://vimeo.com/') ||
/\.(mp4|webm|ogg)$/i.test(text)
);
}
export default function ChatMessage({ content }: { content: string }) {
if (isVideoUrl(content.trim())) {
return <VideoPlayer url={content.trim()} />;
}
return <p>{content}</p>;
}
next.config.jsnext/image domains, add this:// next.config.js
const nextConfig = {
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'Content-Security-Policy',
// Permit YouTube iframes
value: "frame-src 'self' https://www.youtube.com https://player.vimeo.com;",
},
],
},
];
},
};
module.exports = nextConfig;
react-player does not work in React Native. Use react-native-youtube-iframe for YouTube, and for general video, use react-native-video.npm install react-native-youtube-iframe
# For general video support later:
npm install react-native-video
// components/VideoPlayer.native.tsx
import React, { useRef } from 'react';
import { View, StyleSheet } from 'react-native';
import YoutubePlayer from 'react-native-youtube-iframe';
function extractYouTubeId(url: string): string | null {
const match = url.match(
/(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
);
return match ? match[1] : null;
}
interface VideoPlayerProps {
url: string;
}
export default function VideoPlayer({ url }: VideoPlayerProps) {
const videoId = extractYouTubeId(url);
if (!videoId) return null;
return (
<View style={styles.container}>
<YoutubePlayer
height={220}
videoId={videoId}
play={false}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
width: '100%',
borderRadius: 8,
overflow: 'hidden',
},
});
// components/ChatMessage.native.tsx
import React from 'react';
import { Text } from 'react-native';
import VideoPlayer from './VideoPlayer.native';
function isYouTubeUrl(text: string): boolean {
return (
text.includes('youtube.com/watch') ||
text.includes('youtu.be/')
);
}
export default function ChatMessage({ content }: { content: string }) {
if (isYouTubeUrl(content.trim())) {
return <VideoPlayer url={content.trim()} />;
}
return <Text>{content}</Text>;
}
| Platform | Library | Supports |
|---|---|---|
| Next.js / Web | react-player | YouTube, Vimeo, Twitch, MP4, HLS, DASH, and more |
| React Native | react-native-video | MP4, HLS, local files |
| React Native | react-native-youtube-iframe | YouTube only |
react-native-video handles direct video files. You can combine both - check if the URL is a YouTube URL and render YoutubePlayer, otherwise fall back to react-native-video.react-player, wrap it in a dynamic import with ssr: false, drop <VideoPlayer url={...} /> into your chat message renderer.react-native-youtube-iframe, extract the video ID from the URL, render <YoutubePlayer videoId={...} />.react-player on web already handles virtually any video source, so "any video" support is free on Next.js.