{"id":4016,"date":"2023-11-04T23:14:00","date_gmt":"2023-11-04T23:14:00","guid":{"rendered":"http:\/\/localhost:10003\/how-to-create-a-music-player-app-with-vue-js-and-deezer-api\/"},"modified":"2023-11-05T05:48:23","modified_gmt":"2023-11-05T05:48:23","slug":"how-to-create-a-music-player-app-with-vue-js-and-deezer-api","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-create-a-music-player-app-with-vue-js-and-deezer-api\/","title":{"rendered":"How to Create a Music Player App with Vue.js and Deezer API"},"content":{"rendered":"
In this tutorial, we will learn how to create a music player app using Vue.js and the Deezer API. Vue.js is a popular JavaScript framework for building user interfaces, while the Deezer API provides access to a vast library of music tracks. By the end of this tutorial, you will have a fully functional music player app that can search and play music tracks from Deezer.<\/p>\n
To follow along with this tutorial, you will need:
\n– Basic knowledge of JavaScript and Vue.js
\n– Node.js and npm installed on your machine
\n– A Deezer developer account and an API key<\/p>\n
To get started, let’s create a new Vue.js project using the Vue CLI. Open your terminal and run the following commands:<\/p>\n
$ npm install -g @vue\/cli\n$ vue create music-player-app\n<\/code><\/pre>\nFollow the prompts to select the appropriate options for your project. Once the project is created, navigate into the project directory and start the development server:<\/p>\n
$ cd music-player-app\n$ npm run serve\n<\/code><\/pre>\nOpen your browser and visit http:\/\/localhost:8080<\/a> to verify that the Vue.js project is set up correctly.<\/p>\nInstalling dependencies<\/h2>\n
Our music player app will rely on a few additional packages. Run the following command in your terminal to install them:<\/p>\n
$ npm install axios deezer-api vuex --save\n<\/code><\/pre>\nLet’s go over what these packages do:
\n– axios<\/code> is a promise-based HTTP client used for making API requests.
\n– deezer-api<\/code> is a wrapper around the Deezer API, providing an easy-to-use interface for retrieving music tracks.
\n– vuex<\/code> is a state management pattern and library for Vue.js applications.<\/p>\nCreating the Deezer API configuration file<\/h2>\n
Before we can make API requests, we need to store our Deezer API key securely. Create a new file named config.js<\/code> in the src<\/code> directory and add the following code:<\/p>\nexport const DEEZER_API_KEY = '<your_api_key>';\n<\/code><\/pre>\nReplace <your_api_key><\/code> with your actual Deezer API key. Make sure to keep this key private and never commit it to a public repository.<\/p>\nCreating the music search component<\/h2>\n
In the src<\/code> directory, create a new directory called components<\/code>. Inside the components<\/code> directory, create a new file named MusicSearch.vue<\/code>. Open MusicSearch.vue<\/code> and add the following code:<\/p>\n<template>\n <div>\n <input type=\"text\" v-model=\"query\" placeholder=\"Search for music tracks\">\n <button @click=\"search\">Search<\/button>\n\n <ul>\n <li v-for=\"track in tracks\" :key=\"track.id\">\n {{ track.title }} by {{ track.artist }}\n <\/li>\n <\/ul>\n <\/div>\n<\/template>\n\n<script>\nimport { DeezerAPI } from 'deezer-api';\nimport { DEEZER_API_KEY } from '@\/config';\n\nexport default {\n data() {\n return {\n query: '',\n tracks: []\n };\n },\n methods: {\n search() {\n const deezerAPI = new DeezerAPI(DEEZER_API_KEY);\n deezerAPI.searchTracks(this.query)\n .then(response => {\n this.tracks = response.data;\n })\n .catch(error => {\n console.error(error);\n });\n }\n }\n};\n<\/script>\n<\/code><\/pre>\nLet’s break down what this code does:
\n– We import the DeezerAPI<\/code> class from the deezer-api<\/code> package and our DEEZER_API_KEY<\/code> from the config.js<\/code> file.
\n– Inside the data<\/code> function, we define the query<\/code> and tracks<\/code> properties, which will be used to store the user’s search query and the retrieved music tracks, respectively.
\n– The search<\/code> method is triggered when the user clicks the “Search” button. It calls the searchTracks<\/code> method from the DeezerAPI<\/code> class to search for music tracks that match the user’s query. Upon success, the retrieved tracks are stored in the tracks<\/code> property.
\n– Finally, we display the search input, search button, and the list of tracks using directives such as v-model<\/code> and v-for<\/code>.<\/p>\nAdding the music search component to the app<\/h2>\n
Open the src\/App.vue<\/code> file and replace its contents with the following code:<\/p>\n<template>\n <div id=\"app\">\n <MusicSearch \/>\n <\/div>\n<\/template>\n\n<script>\nimport MusicSearch from '.\/components\/MusicSearch.vue';\n\nexport default {\n components: {\n MusicSearch\n }\n};\n<\/script>\n<\/code><\/pre>\nNow our music search component is included in the app’s template. If you restart the development server and visit http:\/\/localhost:8080<\/a>, you should see the search input and button.<\/p>\nPlaying music tracks<\/h2>\n
Let’s enhance our app by adding the ability to play music tracks. Update the MusicSearch.vue<\/code> file with the following code:<\/p>\n<template>\n <div>\n <input type=\"text\" v-model=\"query\" placeholder=\"Search for music tracks\">\n <button @click=\"search\">Search<\/button>\n\n <ul>\n <li v-for=\"track in tracks\" :key=\"track.id\">\n {{ track.title }} by {{ track.artist }}\n <button @click=\"play(track)\">Play<\/button>\n <\/li>\n <\/ul>\n <\/div>\n<\/template>\n\n<script>\nimport { DeezerAPI } from 'deezer-api';\nimport { DEEZER_API_KEY } from '@\/config';\n\nexport default {\n data() {\n return {\n query: '',\n tracks: []\n };\n },\n methods: {\n search() {\n const deezerAPI = new DeezerAPI(DEEZER_API_KEY);\n deezerAPI.searchTracks(this.query)\n .then(response => {\n this.tracks = response.data;\n })\n .catch(error => {\n console.error(error);\n });\n },\n play(track) {\n const deezerAPI = new DeezerAPI(DEEZER_API_KEY);\n deezerAPI.getTrackUrl(track.id)\n .then(response => {\n const audio = new Audio(response.data);\n audio.play();\n })\n .catch(error => {\n console.error(error);\n });\n }\n }\n};\n<\/script>\n<\/code><\/pre>\nIn this updated code, we’ve made the following changes:
\n– Inside the tracks loop, we added a “Play” button to each track. This button triggers the play<\/code> method when clicked and passes the respective track as an argument.
\n– The play<\/code> method calls the getTrackUrl<\/code> method from the DeezerAPI<\/code> class to obtain the URL of the track’s audio file. It then creates an instance of the Audio<\/code> class, sets the audio source to the obtained URL, and calls the play<\/code> method to start playing the track.<\/p>\nNow, when you search for music tracks and click the “Play” button next to a track, the respective track should start playing.<\/p>\n
Conclusion<\/h2>\n
Congratulations! You have created a music player app using Vue.js and the Deezer API. In this tutorial, you learned how to set up a Vue.js project, make API requests with the Deezer API, and play music tracks using the Audio class. With this knowledge, you can further enhance the app by adding features such as a playlist, volume control, and track seek functionality. Remember to always properly handle error cases and enhance the user experience based on your requirements. Enjoy exploring the world of music with your new app!<\/p>\n","protected":false},"excerpt":{"rendered":"
Introduction In this tutorial, we will learn how to create a music player app using Vue.js and the Deezer API. Vue.js is a popular JavaScript framework for building user interfaces, while the Deezer API provides access to a vast library of music tracks. By the end of this tutorial, you Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[261,10,846,599,848,847,849,49,507,773],"yoast_head":"\nHow to Create a Music Player App with Vue.js and Deezer API - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n