Ini merupakan artikel pertama saya tentang pemrograman Delphi. Meski keluarnya tutorial ini agak telat beberapa hari dari jadwal sebelumnya. Mudah-mudahan dapat bermanfaat :)
ilustrasi : shiro player
Pengenalan dan Persiapan
Berbeda dengan VB yang biasanya kita menggunakan component WindowsMedia Player untuk membuat MP3 Player. Dalam Delphi sudah ada component untuk membuat Player. Yaitu component TMediaPlayer yang ada di Tab System. Berikut adalah contoh dari icon TMediaPlayer yang ada di tab systemicon TMediaPlayer pada tab System.
Ok, sekarang buat form baru dan masukan bahan-bahan
- 1 BitBtn dari tab Additional, Properties (name : btnOpenFolder, Glyph : gambar pilihan anda)
- 1 StaticText dari tab Additional, Properties (name : txtFolder, Border Style : sbsSunken)
- 1 ListBox dari tab Standard, Properties (name : mp3List, Hint : Click to play)
- 1 Progressbar, Properties (name:Progress)
- 1 Timer, Properties (nama : ProgresTimer)
- 1 GroupBox yang berisi 6 TLabel dan 6 TEdit. Beri nama pada masing-masing TEdit sesusai gambar berikut, yaitu edTitle, edArtist, edAlbum, edYear, edGenre, dan edComment.
- 1 TMediaPlayer dari tab System, Properties ( name: mp3player)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
| { Article: Membuat MP3 Player Sederhana dari Delphi http: //replace.web.id referensi delphi.about.com } unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, MPlayer, ComCtrls, ExtCtrls; type TForm1 = class (TForm) mp3player: TMediaPlayer; mp3List: TListBox; btnOpenFolder: TBitBtn; GroupBox1: TGroupBox; edTitle: TEdit; edArtist: TEdit; edAlbum: TEdit; edYear: TEdit; edGenre: TEdit; edComment: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; txtFolder: TStaticText; Progress: TProgressBar; ProgresTimer: TTimer; procedure btnOpenFolderClick(Sender: TObject); procedure mp3ListClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ProgresTimerTimer(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; 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} ); implementation uses ShellAPI, ShlObj; // needed for the BrowseForFolder function {$R *.DFM} procedure FillID3TagInformation(mp3File:string; Title,Artist,Album,Year,Genre,Comment:TEdit); 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.Text:= 'Wrong or no ID3 tag information' ; Artist.Text:= 'Wrong or no ID3 tag information' ; Album.Text:= 'Wrong or no ID3 tag information' ; Year.Text:= 'Wrong or no ID3 tag information' ; Genre.Text:= 'Wrong or no ID3 tag information' ; Comment.Text:= 'Wrong or no ID3 tag information' ; end else begin Title.Text:=ID3.Title; Artist.Text:=ID3.Artist; Album.Text:=ID3.Album; Year.Text:=ID3.Year; if ID3.Genre in [0..MaxID3Genre] then Genre.Text:=ID3Genre[ID3.Genre] else Genre.Text:=IntToStr(ID3.Genre); Comment.Text:=ID3.Comment end; end; procedure ChangeID3Tag(NewID3: TID3Rec; mp3FileName: string); var fMP3: file of Byte; OldID3 : TID3Rec; begin try AssignFile(fMP3, mp3FileName); Reset(fMP3); try Seek(fMP3, FileSize(fMP3) - 128); BlockRead(fMP3, OldID3, SizeOf(OldID3)); if OldID3.Tag = 'TAG' then { Replace old tag } Seek(fMP3, FileSize(fMP3) - 128) else { Append tag to file because it doesn't exist } Seek(fMP3, FileSize(fMP3)); BlockWrite(fMP3, NewID3, SizeOf(NewID3)); finally end; finally CloseFile(fMP3); end; end; procedure FillMP3FileList(Folder: string; sl: TStrings); var Rec : TSearchRec; begin sl.Clear; if SysUtils.FindFirst(Folder + '*.mp3' , faAnyFile, Rec) = 0 then try repeat sl.Add(Rec.Name); until SysUtils.FindNext(Rec) <> 0; finally SysUtils.FindClose(Rec); end; end; function BrowseDialog( const Title: string; const Flag: integer): string; var lpItemID : PItemIDList; BrowseInfo : TBrowseInfo; DisplayName : array[0..MAX_PATH] of char ; TempPath : array[0..MAX_PATH] of char ; begin Result:= '' ; FillChar(BrowseInfo, sizeof (TBrowseInfo), #0); with BrowseInfo do begin hwndOwner := Application.Handle; pszDisplayName := @DisplayName; lpszTitle := PChar(Title); ulFlags := Flag; end; lpItemID := SHBrowseForFolder(BrowseInfo); if lpItemId <> nil then begin SHGetPathFromIDList(lpItemID, TempPath); Result := IncludeTrailingBackslash(TempPath); GlobalFreePtr(lpItemID); end; end; procedure TForm1.btnOpenFolderClick(Sender: TObject); var mp3Folder : string; begin mp3Folder := BrowseDialog( 'Choose a folder with mp3 files' , BIF_RETURNONLYFSDIRS); if mp3Folder = '' then Exit; txtFolder.Caption := mp3Folder; //fill the list box with mp3 files FillMP3FileList(mp3Folder, mp3List.Items); end; procedure TForm1.mp3ListClick(Sender: TObject); var mp3File:string; begin if mp3List.Items.Count=0 then exit ; mp3File := Concat(txtFolder.Caption, mp3List.Items.Strings[mp3List.ItemIndex]); if not FileExists(mp3File) then begin ShowMessage( 'MP3 file ' +#13#10+ mp3File +#13#10+ 'does not exist!' ); exit ; end; FillID3TagInformation(mp3File, edTitle, edArtist, edAlbum, edYear, edGenre, edComment); Progress.Max:=0; mp3player.Close; mp3player.FileName:=mp3File; mp3player.Open; Progress.Max := mp3player.Length; end; procedure TForm1.FormCreate(Sender: TObject); begin txtFolder.Caption := ExtractFilePath(Application.ExeName); FillMP3FileList(txtFolder.Caption, mp3List.Items); Progress.Max:=0; end; procedure TForm1.ProgresTimerTimer(Sender: TObject); begin if Progress.Max<>0 then Progress.Position := mp3player.Position; end; end. |