Unverified Commit aef282ff authored by Dylan Jhaveri's avatar Dylan Jhaveri Committed by GitHub

Add example with-hls-js (#12723)

* npm init next-app

* add example video-player component with HLS.js

* add README for example with HLS.js

* update name in package.json

* Update examples/with-hls-js/.gitignore
Co-authored-by: 's avatarLuis Alvarez D. <luis@vercel.com>

* Update license to MIT
Co-authored-by: 's avatarLuis Alvarez D. <luis@vercel.com>

* Update next version to latest
Co-authored-by: 's avatarLuis Alvarez D. <luis@vercel.com>

* remove unused pages/api
Co-authored-by: 's avatarLuis Alvarez D. <luis@vercel.com>
parent b48eb89d
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# HLS.js example
This example shows how to use Next.js along with [HLS.js](https://github.com/video-dev/hls.js/) to playback HLS videos in all modern browsers.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com/now):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-hls-js)
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npm init next-app --example with-hls-js with-hls-js-app
# or
yarn create next-app --example with-hls-js with-hls-js-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-hls-js
cd with-hls-js
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import { useEffect, useRef } from 'react'
import Hls from 'hls.js'
export default function VideoPlayer({ src }) {
const videoRef = useRef(null)
useEffect(() => {
const video = videoRef.current
if (!video) return
video.controls = true
if (video.canPlayType('application/vnd.apple.mpegurl')) {
// This will run in safari, where HLS is supported natively
video.src = src
} else if (Hls.isSupported()) {
// This will run in all other modern browsers
const hls = new Hls()
hls.loadSource(src)
hls.attachMedia(video)
} else {
console.error(
'This is an old browser that does not support MSE https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API'
)
}
}, [src, videoRef])
return (
<>
<video ref={videoRef} />
<style jsx>{`
video {
max-width: 100%;
}
`}</style>
</>
)
}
{
"name": "with-hls-js",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"hls.js": "^0.13.2",
"next": "latest",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}
import Head from 'next/head'
import VideoPlayer from '../components/video-player'
export default function Home() {
return (
<div className="container">
<Head>
<title>Next.js & HLS.js</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className="title">
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className="description">This is an example with HLS.js</p>
<div className="grid">
<VideoPlayer src="https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8" />
</div>
</main>
<footer>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=with-hls-js&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<img src="/vercel.svg" alt="Vercel Logo" className="logo" />
</a>
<div className="separator">.</div>
<a
href="https://mux.com?utm_source=create-next-app&utm_medium=with-hls-js&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Video by <img src="/mux.svg" alt="Mux Logo" className="logo" />
</a>
</footer>
<style jsx>{`
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
margin-left: 0.5rem;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
footer .separator {
width: 20px;
height: 20px;
text-align: center;
}
a {
color: inherit;
text-decoration: none;
}
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
line-height: 1.5;
font-size: 1.5rem;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono,
DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
.video-wrapper {
max-width: 800px;
}
.logo {
height: 1em;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>
<style jsx global>{`
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
sans-serif;
}
* {
box-sizing: border-box;
}
`}</style>
</div>
)
}
<svg width="71" height="25" viewBox="0 0 71 25" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0)">
<path opacity="0.7" d="M66.7332 22.6532C67.6983 23.6231 69.215 23.6231 70.1112 22.6532C71.0763 21.6834 71.0763 20.1593 70.1112 19.2587L52.3248 1.3856C51.3597 0.415738 49.843 0.415738 48.9468 1.3856C48.0506 2.35546 47.9816 3.87952 48.9468 4.78011L66.7332 22.6532Z" fill="url(#paint0_linear)"/>
<path d="M52.3248 22.8613C51.3597 23.8311 49.843 23.8311 48.9468 22.8613C48.0506 21.8914 47.9816 20.3673 48.9468 19.4667L66.7332 1.59361C67.6983 0.623746 69.215 0.623746 70.1112 1.59361C71.0074 2.56347 71.0763 4.08753 70.1112 4.98812L52.3248 22.8613Z" fill="url(#paint1_linear)"/>
<path opacity="0.7" d="M35.8484 18.566C38.8818 18.566 41.3636 16.0721 41.3636 13.0239V3.1868C41.3636 1.80128 42.3977 0.692871 43.7765 0.692871C45.1552 0.692871 46.1893 1.80128 46.1893 3.1868V12.9547C46.1893 18.7046 41.5704 23.346 35.8484 23.346C34.5386 23.346 33.4355 22.2376 33.4355 20.9214C33.5045 19.6051 34.5386 18.566 35.8484 18.566Z" fill="url(#paint2_linear)"/>
<path opacity="0.7" d="M21.0266 0.900879C19.6479 0.900879 18.6138 2.00929 18.6138 3.3948V21.0601C18.6138 22.4456 19.6479 23.554 21.0266 23.554C22.4054 23.554 23.4395 22.4456 23.4395 21.0601V3.3948C23.4395 2.00929 22.4054 0.900879 21.0266 0.900879Z" fill="url(#paint3_linear)"/>
<path d="M43.7767 5.54217C45.1092 5.54217 46.1895 4.45662 46.1895 3.11752C46.1895 1.77842 45.1092 0.692871 43.7767 0.692871C42.4441 0.692871 41.3638 1.77842 41.3638 3.11752C41.3638 4.45662 42.4441 5.54217 43.7767 5.54217Z" fill="url(#paint4_linear)"/>
<path d="M68.595 23.5539C69.9276 23.5539 71.0079 22.4683 71.0079 21.1292C71.0079 19.7901 69.9276 18.7046 68.595 18.7046C67.2624 18.7046 66.1821 19.7901 66.1821 21.1292C66.1821 22.4683 67.2624 23.5539 68.595 23.5539Z" fill="url(#paint5_linear)"/>
<path d="M3.10233 0.900879C1.72354 0.900879 0.689453 2.00929 0.689453 3.3948V21.0601C0.689453 22.4456 1.72354 23.554 3.10233 23.554C4.48112 23.554 5.51521 22.4456 5.51521 21.0601V3.3948C5.51521 2.00929 4.48112 0.900879 3.10233 0.900879Z" fill="url(#paint6_linear)"/>
<path d="M21.0266 23.5539C22.3592 23.5539 23.4395 22.4683 23.4395 21.1292C23.4395 19.7901 22.3592 18.7046 21.0266 18.7046C19.6941 18.7046 18.6138 19.7901 18.6138 21.1292C18.6138 22.4683 19.6941 23.5539 21.0266 23.5539Z" fill="url(#paint7_linear)"/>
<path opacity="0.7" d="M10.2721 13.786C11.2373 14.7559 12.7539 14.7559 13.6501 13.786C14.5463 12.8162 14.6153 11.2921 13.6501 10.3915L4.75695 1.45493C3.86073 0.485074 2.34407 0.485074 1.37891 1.45493C0.413763 2.42479 0.482702 3.94886 1.37891 4.84944L10.2721 13.786Z" fill="url(#paint8_linear)"/>
<path d="M13.7882 13.786C12.823 14.7559 11.3064 14.7559 10.4102 13.786C9.51395 12.8162 9.44501 11.2921 10.4102 10.3915L19.3033 1.45493C20.2685 0.485074 21.7852 0.485074 22.6814 1.45493C23.6465 2.42479 23.6465 3.94886 22.6814 4.84944L13.7882 13.786Z" fill="url(#paint9_linear)"/>
<path d="M35.8487 18.566C32.8154 18.566 30.3336 16.0721 30.3336 13.0239V3.1868C30.3336 1.80128 29.2995 0.692871 27.9207 0.692871C26.5419 0.692871 25.5078 1.80128 25.5078 3.1868V12.9547C25.5078 18.7046 30.1268 23.346 35.8487 23.346C37.1586 23.346 38.2616 22.2376 38.2616 20.9214C38.1927 19.6051 37.1586 18.566 35.8487 18.566Z" fill="url(#paint10_linear)"/>
</g>
<defs>
<linearGradient id="paint0_linear" x1="-0.000211689" y1="12.0227" x2="70.8359" y2="12.0227" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint1_linear" x1="0.861528" y1="12.2242" x2="70.8359" y2="12.2242" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint2_linear" x1="72.4818" y1="12.0227" x2="0.754128" y2="12.0227" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint3_linear" x1="0.142208" y1="12.2242" x2="71.8699" y2="12.2242" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint4_linear" x1="0.632025" y1="3.10346" x2="70.894" y2="3.10346" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint5_linear" x1="-0.747154" y1="21.1433" x2="70.9803" y2="21.1433" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint6_linear" x1="-0.0302055" y1="12.2242" x2="71.6975" y2="12.2242" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint7_linear" x1="0.0345948" y1="21.1292" x2="72.1801" y2="21.1292" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint8_linear" x1="1.03422" y1="7.62047" x2="71.5604" y2="7.62047" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint9_linear" x1="0.862056" y1="7.62047" x2="69.9773" y2="7.62047" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<linearGradient id="paint10_linear" x1="-0.400385" y1="12.0227" x2="71.3273" y2="12.0227" gradientUnits="userSpaceOnUse">
<stop stop-color="#FF4E00"/>
<stop offset="1" stop-color="#FF1791"/>
</linearGradient>
<clipPath id="clip0">
<rect width="71" height="24.85" fill="white"/>
</clipPath>
</defs>
</svg>
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
</svg>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment