An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
January 20, 2023 12:56 pm GMT
Original Link: https://dev.to/eliaschen/build-a-music-player-with-youtube-iframe-player-api-ncp
Build a music player with YouTube IFrame Player API
I added a musicplayer based on YouTube IFrame Player API for my personal site music page to play my playlist from YTmusic.
Stack
- React as a framework
- YouTube Data API v3 (fetching music list from youtube)
- react-youtube Youtube iframe player for react
react-youtube
Add player config.
import YouTube from 'react-youtube';const opts = { height: "250", width: "350", playerVars: { autoplay: 1, },};
Add player from react-youtube.
<YouTube videoId={"UEx5T0xfUk1td3F5ZFJ0elRhVHV6SGM3R0NYbEFSMmFPOC5EMEEwRUY5M0RDRTU3NDJC"} //Video id from youtube opts={opts}/>;
Create a control player event value
let videoElement: YouTubePlayer = null
Player event control like:pauseVideo()
, playVideo()
\
Example of video play button:
<button onClick={()=>videoElement.target.playVideo()}> Play Video</button>
PlayList
Create a API token on google api console, search and add YouTube Data API v3.
import React from 'react';const [loading, setLoading] = React.useState(true);const [playList ,setPlaylist] = React.useState(null);React.useEffect(() => { setLoading(true); fetch( "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLyOL_RMmwqydRtzTaTuzHc7GCXlAR2aO8&key=<your_api_token>&maxResults=1000", {} ) .then((res) => res.json()) .then((data) => { setPlaylist(data.items); setLoading(false); });}, []);return ( <ul> {loading || !playList ? ( <h1>Loading...</h1> ) : ( playList.map((items) => <li key={items.id}>{items.snippet.title}</li>) )} </ul>);
Source code: Github
Website: Personal site
Thanks for reading, see you in the next blog.
Original Link: https://dev.to/eliaschen/build-a-music-player-with-youtube-iframe-player-api-ncp
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To