DEV Community

Cover image for Adding YT Video to JANA
jtwebguy
jtwebguy

Posted on

2

Adding YT Video to JANA

A while ago I created a tech/biz/gaming/entertainment news aggregator named JANA which stands for just another news aggregator created in Ionic and React. Video seems just right for an addition so here's how I made it.

npm i yt-player
Enter fullscreen mode Exit fullscreen mode

First, we need to install yt-player npm package. This does not have typings as I'm creating this with TypeScript so I generated one using this instruction. After generating, copy your *.ts files to the root of the yt-player folder.

This code below will be the main YT Frame API Component.

export const YTAPi = ({props, vid}:any) =>{
    let o:any = null
    const [player, setPlayer] = useState(o)
    const [time, setTime] = useState(0)

    useEffect( () =>{

            let p:any = document.querySelector('#playerMain')
            if(p == null) return

            if(player == null){
                setPlayer(new YouTubePlayer('#playerMain',{autoplay:true}))

            }else{
                player.load(vid ? vid : 'GKSRyLdjsPA')

                    player.on('cued', ()=>{
                        player.play()
                    })
            }

    }, [player])


    useEffect( () =>{

            if(player == null) return
            player.load(vid)
                    //player.setVolume(100)


                    player.on('playing', () => {
                        setTime(player.getDuration()) // => 351.521
                    })

                    player.on('ended', ()=>{
                        //alert('ended')
                    })
                    player.on('cued', ()=>{
                      player.play()
                  })

    }, [vid])

    return (
        <>
        <div id="playerMain" className="mainPlayer"></div>
        </>
    )
}
Enter fullscreen mode Exit fullscreen mode

The "{props,vid}" arguments take object and a video ID string so we can play other videos as well.

Image description

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay