CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

How To Create A Music Player With The Python Delphi Ecosystem

Music is more than sound; it’s an emotion, a narrative, a refuge. Imagine wielding the power to design your musical universe, where every note and rhythm aligns with your desires. In this post, you will see the synergy of Python and Delphi, where we will unveil a music player that transcends expectations.

This music player app will have an option for users to create playlists as well as play, stop, and pause songs. Users can even monitor song durations and real-time timestamps. Users can easily adjust the volume and navigate through their music in 10-second intervals. Throughout this journey, we’ll introduce you to advanced Delphi components that seamlessly integrate Python’s versatility with Delphi’s strength.

How to Build a Feature-Packed Music Player With DelphiFMX? 

How To Create A Music Player With The Python Delphi Ecosystem

Let’s go over the building of a music player app in a step by step manner.

What Are the Prerequisites for this Project?

Before we dive into building our feature-packed music player with DelphiFMX, let’s ensure we have all the necessary tools and components in place.

First and foremost, make sure you have Delphi Software installed. Delphi will be our creative canvas, allowing us to craft a music player with an enticing user interface that sings with functionality.

Next up, you’ll need the Delphi4PythonExporter tool. This nifty utility acts as the bridge connecting Delphi and Python, making the integration seamless and painless.

You’ll require a text editor or an integrated development environment (IDE) that supports Python programming to code in Python. We highly recommend PyScripter, a user-friendly environment that harmoniously blends Python coding within Delphi. If you haven’t already set up these tools, consider exploring our article “Powerful Python GUI Project Setup” to help you get started on the right foot.

Finally, you will need a codec pack to playback of all your music and video files. For this, we will need the K-Lite Codec Pack, which is a free software bundle shipped with decoders needed to view any movie, video, or music file. For this tutorial, you will need to download and install the basic codec pack from their official website.

With these prerequisites in place, you’ll be all set to embark on the journey of creating a feature-packed music player that will elevate your music experience to new heights. And if you’re eager to dive right in, you can access the complete code and resources for this tutorial from our GitHub repository at the following link:

https://github.com/Embarcadero/PythonBlogExamples/tree/main/Music_Player

How to Use the Delphi IDE to Create a Stunning GUI for this App?

Let’s start by launching our Delphi app and creating a new application. Search for the Create New tab on your welcome page and click on Multi-Device Application. Next, opt for the Blank Application and click on OK. For this tutorial, we’ve named our project “MusicPlayerProject.”

If you’re not well-acquainted with the different sections of the Delphi IDE, we suggest referring to our complimentary eBook bundle. This resource comprehensively covers the Delphi Python EcoSystem and all Python GUI offerings.

How To Create A Music Player With The Python Delphi Ecosystem

Now, we will start off by naming our form. Right-click on the form and select Quick Edit. Here, we will set the name of our form as MusicPlayerWindow with the caption “Music Player.” We will also rename the source file of our project from Unit1.pas to MusicPlayer.pas to help us keep track.

How To Create A Music Player With The Python Delphi Ecosystem

Next, let’s resize our form. Select your form and head over to the Object Inspector. Under the properties tab, search for the ClientWidth and ClientHeight properties and adjust the sizes accordingly.

We will add a few tabs to the form for this project. So, head over to the Palette and search for the TTabControl object. Drag and Drop this object to your form and adjust its size accordingly. Next, right-click on the TTabControl object and add two tabs to the form: Songs and Now Playing. The Songs tab will display all the songs in the app, while the Now Playing tab will serve as the main music player. We will also add a TRectangle component to the top of the form that will serve as the placeholder for the title and a button (which we will define later).

Here is what our form looks like:

How To Create A Music Player With The Python Delphi Ecosystem

Note: We will be customizing the text style of each label using the TextSettings property of the component. This will help us customize the feel of the form.

Now, let’s add a few labels to the form. Search for the TLabel component in the Palette and add it to the form. We will add a few of these components to indicate multiple things, including filename, song directory, artist, and time. We will also use them as placeholders to indicate multiple other components. Here is what our form looks like:

How To Create A Music Player With The Python Delphi Ecosystem

Next, let’s add a few buttons. Head to the Palette, search for the TButton component and add a few buttons to the form. Here, we will add a Create Playlist button, forward and backward buttons (which will change the time by 10 seconds), and Pause, Play, and Stop for the music player. Additionally, for each player button, we are going to customize their appearance by heading over to the Object Inspector and changing their style using the StyleLookup property:

How To Create A Music Player With The Python Delphi Ecosystem

This is what our form looks like so far:

How To Create A Music Player With The Python Delphi Ecosystem

Now, let’s add a few TTrackBar components. One track bar component will help adjust the volume of our music player, while the other will serve as a tracker to track the progress of our music. We will also add a TImage component that will help display the album cover of our music file. Here is what our form looks like:

How To Create A Music Player With The Python Delphi Ecosystem

Finally, we’ll add a TOpenDialog to help select files we want to play. We will also add a TTimer and a TMediaPlayer component to help us play our music files. The placement of these components does not matter as they will be invisible:

How To Create A Music Player With The Python Delphi Ecosystem

Now that our Now Playing tab is complete, head over to the Songs tab and add a TListBox component. This will serve as a placeholder for all our music files.

How To Create A Music Player With The Python Delphi Ecosystem

Now that our form is ready, the last step is to add functionality to each button. To do this, double-click on each button on the form. This will generate an OnClick method for that button and create a procedure in the .pas file, enabling us to program the button.

How To Create A Music Player With The Python Delphi Ecosystem

Due to the Delphi4PythonExporter’s lack of runtime implementation, we must include at least one comment (//) in each procedure to preserve its function when we export the form.

How To Create A Music Player With The Python Delphi Ecosystem

How to Add Functionality to this GUI?

With our forms now ready, we can begin exporting our forms. Navigate to Tools > Export To Python and click on Export Current Entire Project.

How To Create A Music Player With The Python Delphi Ecosystem

Here we will give our project the title Music Player. Now, select the directory of your choosing and click on Export.

How To Create A Music Player With The Python Delphi Ecosystem

Delphi will generate two Python and one .pyfmx file. The MusicPlayerProject.py file will be used to run the application, whereas the MusicPlayer.py file will contain the class of our form. The MusicPlayer.pyfmx contains the visual information of our form. Below are the contents of each Python file.

MusicPlayerProject.py:

MusicPlayer.py:

The only thing we need to do now is start adding functionality to our forms. Start by opening up MusicPlayer.py in the text editor of your choice and importing the TinyTag library:

Next, head over to the __init__ function and start by adding a custom style for the form. You’ll first need to obtain the free eBook bundle, which includes various styles. Download the eBook and extract the FMX styles ZIP file to your preferred directory. Then select the style you wish to use; in this instance, we’ll opt for transparent.style from the bundle. In addition, we will also set the default value of the volume bar to 100. Our __init__ function looks like:

Now, let’s program our Play, Pause, and Stop buttons. Head over to the play_btnClick function and start by turning the button off after it is pressed. Next, we will enable the Stop and Pause buttons, while calling the media player to play the music. Here is what our function looks like:

Similarly, we can add functionalities for our Stop and Pause buttons. Here is what the functions look like:

Next, let’s add some functionality to the forward and backward buttons. Navigate to the fwd_btnClick function and start by getting the CurrentTime of the music:

Now add 10 seconds to the current time and then check if the duration is less than the duration of the song. If this condition is satisfied, we set the current time to the new time. Here is what the function looks like:

Similarly, we can program the bwd_btnClick function but instead check if the new time is less than 0. Here is what the function looks like:

Next, lets add some functionality to the volume track bar. Head over to the volumeChange function and start by getting the current volume stated in the bar. Next, assign the new volume value to the media player. Here is our final volumeChange function:

Now, let’s head over to the update_current_time function and use it to display the current time using the media_player. We will also display the current time in our current_time_label:

Next, we will move on to the create_playlistClick function. We will use this function to populate our playlist in the Songs tab. So, we will start by opening up the dialog box for file selection. Next, if the user selects the song, we will append it to the ListBoxItem and add it to our playlist. Here is what the function looks like:

We are finally ready to add the main code to our Media player. Head over to the audio_list_boxChange function and start by retrieving the item selected in the list box:

Next, change the tab to the Now Playing tab and use TinyTag to retrieve information about the selected song:

Next, set the various labels we have with information about the song and update the embedded image if there is one available:

Finally, load and play the selected audio file. Here is what our function looks like:

Here is what our final MusicPlayer.py file looks like:

What Are the Final Results?

Now that everything is complete, it’s time to test out our app. Open up MusicPlayerProject.py and run the file.

How To Create A Music Player With The Python Delphi Ecosystem

Next, click the Create Playlist button and select the files you want to play. Now navigate to the Songs tab to see your playlist:

How To Create A Music Player With The Python Delphi Ecosystem

Finally, double-click on a song to start playing:

How To Create A Music Player With The Python Delphi Ecosystem

What Have You Learned in this Article?

How To Create A Music Player With The Python Delphi Ecosystem

Conclusively, in this tutorial, we’ve transformed a basic music player into a versatile tool. You’ve learned to craft playlists effortlessly, fine-tune playback, and enhance your music experience with song duration and timestamps.

But this is just the beginning. Delphi opens doors to endless possibilities. Whether you’re a novice or an experienced developer, it’s time to harness the power of Delphi. Elevate your software development skills, create exceptional applications, and leave your digital mark.

Delphi offers boundless opportunities. So, why wait? Dive into the world of Delphi today and turn your ideas into reality. Your next innovation is just a step away.

What Are Some Relevant FAQs?

How To Create A Music Player With The Python Delphi Ecosystem

What is DelphiFMX, and why should I use it for building a music player?

DelphiFMX is a framework for creating cross-platform applications with Delphi. It provides a powerful and user-friendly way to build applications with rich user interfaces. Using it for a music player allows you to create a visually appealing and feature-packed player easily.

Can I use other programming languages besides Python with DelphiFMX?

DelphiFMX primarily supports the Delphi programming language, but you can use external libraries like the Python4Delphi bridge to incorporate Python into your Delphi applications, as demonstrated in this tutorial.

Are there any licensing requirements for using Delphi and Python together?

Delphi and Python can be used together without any additional licensing requirements. However, make sure to review the licenses of any third-party libraries or components you use in your project.

Can I customize the appearance and behavior of Delphi components to match the design of my music player?

Yes, Delphi components are highly customizable. You can adjust properties such as colors, fonts, sizes, and styles to achieve the desired look and feel for your music player’s user interface.

What are some commonly used Delphi components for building a music player?

For a music player, you might use components like TMediaPlayer, TListBox for playlists, TButton for playback controls, TProgressBar for tracking song progress, and TTrackBar for volume control, among others.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *