1. Home
  2. Computing & Technology
  3. Delphi Programming
Your first MP3 Delphi player
Part 3: Building a MP3 player Delphi Project's Code
 More of this Feature
• Part 1: Delphi plays MP3
• Part 2: mp3Player GUI
• Part 4: ID3 Tag Editing
• Part 5: Project's Code
 Join the Discussion
"I am trying to convert .mp3 to .wav in my delphi program..."
Discuss the possibilities
  Related Resources
• mp3 inside an exe
• Multimedia VCL/Tools
• Records in Object Pascal
• File I/O with Delphi
• BrowseForFolder dialog
 From Other Guides
• About MP3/MIDI Music
 Elsewhere on the Web
• Setting the sound volume of a TMediaPlayer
• TMediaPlayer in Action
• TMediaPlayer.help
 

Even the simpliest project needs some code to run. Till this moment we were only designing our projects GUI - it's time to see our player in action.

Select the folder with MP3 files
As stated before, the btnOpenFolder and the txtFolder are used to select and display the Folder where our MP3 songs are stored. To enable a user to select a list of mp3 songs from a directory on a system Delphi provides us with several possibilities. The OpenDialog component located on the Dialogs tab encapsulates the standard Windows dialog box for opening a file. Another approach is to use the SHBrowseForFolder Windows API function and to invoke a Windows system dialog used to browse for files and folders on users hard drive as well as network computers and printers. This second approach is exactly what the btnOpenFolder hides in his OnClick event. For a full code see the last part of this article: Project's Code.

All the MP3 files from the selected folder are listed in the mp3List (that is the ListBox component). To list all the MP3 files in a given directory we use techniques described in the Searching For Files article.

Display play progress
The trick is simple. The TMediaPlayer Position property holds the current position of a song. The Lenght property specifies the length of the song selected by using the current time format, which is specified by the TimeFormat property. When the user selects a song from the list the following assignement is made

...
 Progress.Max := 0;
 {code to open a mp3 song}
 Progress.Max := mp3player.Length;
...

and in the OnTimer event for the Timer (named 'ProgresTimer') component we have:

procedure TForm1.ProgresTimerTimer
         (Sender: TObject);
begin
 if Progress.Max <> 0 then
   Progress.Position := mp3player.Position;
end;

Play the MP3 file
You would not believe this, but this is the simplest part. Since, by default, TMediaPlayer knows what to do when the user chooses Play or Stop button - everything we have to do is to prepare the MediaPlayer to accept/open a MP3 file. I'll give you here the whole procedure - the one that is executed when the user select's a song from the list:

procedure TForm1.mp3ListClick(Sender: TObject);
 var mp3File:string;
begin
 //if the list is empty don't do anything
 if mp3List.Items.Count=0 then exit;
 //file is FolderName + FileName
 mp3File := Concat(txtFolder.Caption,
                   mp3List.Items.Strings
                       [mp3List.ItemIndex]);

  //Chechk again if it exists
 if not FileExists(mp3File) then begin
  ShowMessage('MP3 file does not exist?!');
  exit;
 end;

 //used to display the ID3 tag information
 FillID3TagInformation (mp3File,
                        edTitle,
                        edArtist,
                        edAlbum,
                        edYear,
                        edGenre,
                        edComment);

 Progress.Max:=0;

 mp3player.Close;
 mp3player.FileName:=mp3File;
 mp3player.Open;

 Progress.Max := mp3player.Length;
end;

mp3Player at run time

Next page > MP3 ID3 Tag Editing > Page 1, 2, 3, 4, 5

About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >