playing mp3 with TMediaplayer

16:08
Below is the code I used to make a simple MP3 player with the mediaplayer.
I haven't tried actually using the play button on the media player but when
I use my own buttons and do an Open, then a Play it works fine. (initial
code based off of an article on delphi.about.com)

unit Unit1;
 
interface
 
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ImgList, BrowseDr, ComCtrls, dfsStatusBar, ToolWin, dfsToolBar, MPlayer,
ExtCtrls, Menus, GradForm, StdCtrls,
SystemControlPack, SystemComboBox, SystemListView, RzLstBox, RkLabel,
RkPanel, dxfLabel;
 
type
HMSRec = record
H: byte;
M: byte;
S: byte;
N: byte;
end;
 
 
type
TID3Rec = packed record
Tag: array[0..2] of Char;
Title,
Artist,
Comment,
Album: array[0..29] of Char;
Year: array[0..3] of Char;
Genre: Byte;
end;
 
const
MaxID3Genre = 147;
ID3Genre: array[0..MaxID3Genre] of string = (
'Blues', 'Classic Rock', 'Country', 'Dance', 'Disco', 'Funk',
'Grunge',
'Hip-Hop', 'Jazz', 'Metal', 'New Age', 'Oldies', 'Other', 'Pop',
'R&B',
'Rap', 'Reggae', 'Rock', 'Techno', 'Industrial', 'Alternative', 'Ska',
'Death Metal', 'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient',
'Trip-Hop', 'Vocal', 'Jazz+Funk', 'Fusion', 'Trance', 'Classical',
'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip', 'Gospel',
'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk', 'Space', 'Meditative',
'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 'Gothic',
'Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk',
'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta',
'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American',
'Cabaret', 'New Wave', 'Psychadelic', 'Rave', 'Showtunes', 'Trailer',
'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro',
'Musical', 'Rock & Roll', 'Hard Rock', 'Folk', 'Folk-Rock',
'National Folk', 'Swing', 'Fast Fusion', 'Bebob', 'Latin', 'Revival',
'Celtic', 'Bluegrass', 'Avantgarde', 'Gothic Rock', 'Progressive
Rock'
,
'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band',
'Chorus', 'Easy Listening', 'Acoustic', 'Humour', 'Speech', 'Chanson',
'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass',
'Primus',
'Porn Groove', 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba',
'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle',
'Duet', 'Punk Rock', 'Drum Solo', 'Acapella', 'Euro-House', 'Dance
Hall'
,
'Goa', 'Drum & Bass', 'Club-House', 'Hardcore', 'Terror', 'Indie',
'BritPop', 'Negerpunk', 'Polsk Punk', 'Beat', 'Christian Gangsta Rap',
'Heavy Metal', 'Black Metal', 'Crossover', 'Contemporary Christian',
'Christian Rock', 'Merengue', 'Salsa', 'Trash Metal', 'Anime', 'Jpop',
'Synthpop' {and probably more to come}
);
 
const
 
ModeStr: array[TMPModes] of string = ('Not ready', 'Stopped', 'Playing',
'Recording', 'Seeking', 'Paused', 'Open');
 
type
TForm1 = class(TdfsGradientForm)
dfsToolBar1: TdfsToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
dfsStatusBar1: TdfsStatusBar;
dfsBrowseDirectoryDlg1: TdfsBrowseDirectoryDlg;
ToolButton5: TToolButton;
ImageList1: TImageList;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
MediaPlayer1: TMediaPlayer;
Bevel1: TBevel;
MainMenu1: TMainMenu;
File1: TMenuItem;
Play1: TMenuItem;
Stop1: TMenuItem;
N1: TMenuItem;
Rewindtobegining1: TMenuItem;
Forwardtoend1: TMenuItem;
N2: TMenuItem;
E1: TMenuItem;
Hellp1: TMenuItem;
ToolButton8: TToolButton;
dfsSystemListView1: TdfsSystemListView;
dfsSystemComboBox1: TdfsSystemComboBox;
RzTabbedListBox1: TRzTabbedListBox;
RkPanel1: TRkPanel;
RkPanel2: TRkPanel;
RkLabel1: TRkLabel;
RkLabel2: TRkLabel;
RkLabel3: TRkLabel;
RkPanel3: TRkPanel;
RkLabel5: TRkLabel;
RkPanel4: TRkPanel;
Shape1: TShape;
RkLabel4: TRkLabel;
Title: TRkLabel;
RkLabel7: TRkLabel;
Artist: TRkLabel;
Album: TRkLabel;
RkLabel6: TRkLabel;
RkLabel8: TRkLabel;
Year: TRkLabel;
RkLabel9: TRkLabel;
Genre: TRkLabel;
lbComment: TRkLabel;
Comment: TRkLabel;
Label1: TLabel;
Timer1: TTimer;
procedure ToolButton1Click(Sender: TObject);
procedure FillID3TagInformation(mp3File: string);
procedure RzTabbedListBox1DragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
procedure RzTabbedListBox1DragDrop(Sender, Source: TObject; X,
Y: Integer);
procedure dfsSystemListView1DblClick(Sender: TObject);
procedure MediaPlayer1Notify(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure ToolButton4Click(Sender: TObject);
procedure ToolButton5Click(Sender: TObject);
procedure ShowTime(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
 
var
Form1: TForm1;
Continue: boolean;
 
implementation
 
{$R *.DFM}
 
 
procedure TForm1.ShowTime(Sender: TObject);
var
SongLen : LongInt;
begin
MediaPlayer1.TimeFormat := tfHMS;
SongLen:=MediaPlayer1.Position;
with HMSRec(SongLen) do
Label1.Caption := IntToStr(H) + ':' + IntToStr(M) + ':' + IntToStr(S);
end;
 
 
procedure TForm1.FillID3TagInformation(mp3File: string);
var //fMP3: file of Byte;
ID3: TID3Rec;
fmp3: TFileStream;
begin
fmp3 := TFileStream.Create(mp3File, fmOpenRead);
try
fmp3.position := fmp3.size - 128;
fmp3.Read(ID3, SizeOf(ID3));
finally
fmp3.free;
end;
 
{ or the non Stream approach - as in ChangeID3Tag procedure
try
AssignFile(fMP3, mp3File);
Reset(fMP3);
try
Seek(fMP3, FileSize(fMP3) - 128);
BlockRead(fMP3, ID3, SizeOf(ID3));
finally
end;
finally
CloseFile(fMP3);
end;
}

if ID3.Tag <> 'TAG' then
begin
Title.Caption := 'Unknown';
Artist.Caption := 'Unknown';
Album.Caption := 'Unknown';
Year.Caption := 'Unknown';
Genre.Caption := 'Unknown';
Comment.Caption := 'Unknown';
end else
begin
Title.Caption := ID3.Title;
Artist.Caption := ID3.Artist;
Album.Caption := ID3.Album;
Year.Caption := ID3.Year;
if ID3.Genre in [0..MaxID3Genre] then Genre.Caption :=
ID3Genre[ID3.Genre]
else Genre.Caption := IntToStr(ID3.Genre);
Comment.Caption := ID3.Comment
end;
end;
 
procedure TForm1.ToolButton1Click(Sender: TObject);
var
mp3File: TFilename;
begin
if RzTabbedListBox1.Count > 0 then
begin
Continue := True;
RzTabbedListBox1.ItemIndex := 0;
MediaPlayer1.Close;
 
MediaPlayer1.FileName := RzTabbedListBox1.Cells[1,
RzTabbedListBox1.ItemIndex];
FillID3TagInformation(MediaPlayer1.FileName);
//mp3File;
MediaPlayer1.Open;
Timer1.Enabled:=True;
MediaPlayer1.Play;
end;
end;
 
procedure TForm1.RzTabbedListBox1DragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
begin
if source is TdfsSystemListView then Accept := True else Accept := False;
end;
 
procedure TForm1.RzTabbedListBox1DragDrop(Sender, Source: TObject; X,
Y: Integer);
begin
if
Uppercase(ExtractFileExt(dfsSystemListView1.Filename[dfsSystemListView1.Sele
cted])) = '.MP3' then
 
RzTabbedListBox1.Items.Add(dfsSystemListView1.Filename[dfsSystemListView1.Se
lected] + #9 +
dfsSystemListView1.FullFilename[dfsSystemListView1.Selected]);
end;
 
procedure TForm1.dfsSystemListView1DblClick(Sender: TObject);
begin
if
Uppercase(ExtractFileExt(dfsSystemListView1.Filename[dfsSystemListView1.Sele
cted])) = '.MP3' then
 
RzTabbedListBox1.Items.Add(dfsSystemListView1.Filename[dfsSystemListView1.Se
lected] + #9 +
dfsSystemListView1.FullFilename[dfsSystemListView1.Selected]) else
dfsSystemListView1.DefaultDblClickAction(dfsSystemListView1.Selected);
end;
 
procedure TForm1.MediaPlayer1Notify(Sender: TObject);
var
mp3File: TFilename;
 
begin
with Sender as TMediaPlayer do
begin
if (Continue = True) and (ModeStr[Mode] = 'Stopped') and
(RzTabbedListBox1.ItemIndex <> RzTabbedListBox1.Count - 1) then
begin
Timer1.Enabled:=False;
RzTabbedListBox1.ItemIndex := RzTabbedListBox1.ItemIndex + 1;
MediaPlayer1.Close;
MediaPlayer1.FileName := RzTabbedListBox1.Cells[1,
RzTabbedListBox1.ItemIndex];
FillID3TagInformation(MediaPlayer1.FileName);
 
//mp3File;
MediaPlayer1.Open;
Timer1.Enabled:=True;
MediaPlayer1.Play;
end;
// Form1.Caption := ModeStr[Mode];
{ Note we must reset the Notify property to True }
{ so that we are notified the next time the }
{ mode changes }
Notify := True;
end;
end;
 
procedure TForm1.ToolButton2Click(Sender: TObject);
begin
MediaPlayer1.Stop;
Continue := False;
end;
 
procedure TForm1.ToolButton4Click(Sender: TObject);
begin
MediaPlayer1.Rewind;
end;
 
procedure TForm1.ToolButton5Click(Sender: TObject);
begin
MediaPlayer1.Stop;
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
ShowTime(Sender);
end;
 
end.