Build a youtube music player application with JavaScript

Tram Ho

I. About Music Player

Music application – which will lead you into a wonderful and diverse world of music. With the continuous development of technology, listening to music has become easier and more convenient than ever. Here are some popular and great music player apps.

  1. Spotify : With over a million songs and a wide variety of music genres, Spotify is one of the top music apps on the market. You can search, listen and share your favorite music easily.
  2. Apple Music : For users of the Apple ecosystem, Apple Music is the perfect choice. With a rich music store, the latest songs and albums, you can access and listen to music on a variety of Apple devices.
  3. YouTube Music : With YouTube Music, you can experience music from millions of songs and music videos on YouTube. This app provides personalized playlists, recommendations based on your preferences and listening needs.
  4. SoundCloud : SoundCloud is an online music platform that allows users to search and play songs from indie artists and emerging musicians. You can find remixes, unreleased songs, and more unique content on SoundCloud.
  5. Deezer : Deezer offers an extensive music library with millions of songs from many different genres. You can create personal playlists, discover high-quality (lossless) copies, and enjoy an unlimited listening experience.image.png

So why don’t we create our own online music application. Today, let’s create a music player application with JavaScript. The source of the music will be taken from the youtube page.

II. Source code structure

image.png

  • css: Folder to save css files to align and beautify the interface.
  • js: Directory containing JavaScript files to handle game functions.
  • home.html: Music player application interface.

III. Build apps

1. Interface

  • home.html: Music player application interface.

music.css: Add styles to those HTML elements such as changing the layout, page color, text color for the application interface.

I have referenced the interface source code here. Thanks bro for sharing the source code. Music player UI design

Because the html and css interface is not too complicated, you can read and understand it yourself! After a bit of html and css we will have an interface like this.

Desktop application interface

image.png

Mobile application interface

image.png

2. Generate Youtube API key

Before entering the detailed functions. Due to the music source from youtube, everyone generates an api key to get information and play youtube music. The steps to get the api key are as follows:

  • Go to the Google Developers Console page.
  • Create a new project or select an existing project.
  • Enable YouTube Data API for the project.
  • Generate an API key for the project.

3. Handling Youtube APIs

Process to find playlists by keyword search. Here I take the 10 closest songs with keywords according to Youtube search

Get song information by id: title, video channel, video thumbnail,…

These two functions are just calling the Youtube API according to the search keyword. If you read the document of Youtube, it’s okela. No problem with this part.

4. Song search function

In this part, I used the sweet alert library to make the interface more beautiful. The steps are also simple. First we will have the function getInfoTheSong, this function has the function of calling the processing function to get the song information above and display the values ​​on the screen interface.

In the search processing step, we will process each step as follows:

  • Get the user input key search input.
  • Call the searchKey function above to get the song list information.
  • Save the playlist and default selected the first song.
  • Play music.

5. Play/pause function

In this processing step, we check if the state and turn the music on and off corresponds to the current state

6. Function to select previous and next song

Before going to the details of each function. We have a function to get the new position of the song

  • If the user next song => next song => song position increases by 1. However, if the new position exceeds the number of songs in the list => back to the first song => the new position is 0 .
  • If user previous song => previous song => song position subtract 1. However, if new position is less than 0 => go back to last song => new position is list length – 1.

Next song selection function

Previous song selection function

IV. Conclude

Here are some pictures and video results after building the code, everyone ^^

image.png

 

Share the news now

Source : Viblo