unit LoadIt;
interface
{
Original code assisted by Sinisa Vuk Date: April 3, 2017
https://www.experts-exchange.com/members/sinisav.html
His code was clicking on the items in the ListView and having them display to
the TEdits.
Updated: March 15, 2024
Fixed the bug in loading the Song Titles to the ListView.
Added in TEdit strPath. This helps to know how many files are selected.
Fixed the width of each ListView Column so they match the length of their corresponding File information.
}
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, StdCtrls, ID3v1Library, ID3v2Library;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
ListView1: TListView;
OpenDialog1: TOpenDialog;
Button8: TButton;
Edit1: TEdit;
strTitle: TEdit;
strPath: TEdit;
strArtist: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ID3v1Tag: TID3v1Tag = nil;
ID3v2Tag: TID3v2Tag = nil;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Panel2.Align := alClient;
ListView1.Align := alClient;
ID3v1Tag := TID3v1Tag.Create;
ID3v2Tag := TID3v2Tag.Create;
end;
procedure AddFileToTree(ListView: TListView; FileName: String);
var
SR: TSearchRec;
ListItem: TListItem;
begin
// FileSize := 0;
if (FindFirst(FileName, faAnyFile, SR) = 0) then
begin
// FileSize := SR.Size;
// FileDate := FileDateToDateTime(SR.Time);
ListItem := ListView.Items.Add;
// get the MP3 File name
ListItem.Caption := extractfilename(FileName);
// get the song title
ListItem.SubItems.Add(ID3v2Tag.GetUnicodeText('TIT2'));
// get the path to the MP3 file
ListItem.SubItems.Add(FileName);
end;
end;
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
var
s, sFile: string;
ls: TListItem;
begin
ls := ListView1.Selected;
if Assigned(ls) then
begin // Change this number [1] to the column which has the FileName or SongTitle.
// Chosing the File path address, [2] will result in this error.
//List index out of bounds (2). TSubItems object range is 0..1.
sFile := ls.SubItems.Strings[1];
if ID3v2Tag.LoadFromFile(sFile) = ID3V2LIBRARY SUCCESS then
begin
strPath.text := sFile; // Added March 15, 2024
strTitle.text := ID3v2Tag.GetUnicodeText('TIT2');
strArtist.text := ID3v2Tag.GetUnicodeText('TPE1')
end
else
strPath.text := ';
ls := ListView1.GetNextItem(ls, sdAll, [isSelected]);
// if there are more than one
while Assigned(ls) do
begin
// If multiple items are selected, then we want to change the edit to show the value <multi>
strTitle.text := '<multi>';
strArtist.text := '<multi>';
ls := ListView1.GetNextItem(ls, sdAll, [isSelected]);
end;
end;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
i: integer;
LoadIt: integer;
begin
if OpenDialog1.execute then
for i := 0 to OpenDialog1.Files.Count - 1 do
if SameText(ExtractFileExt(OpenDialog1.Files[i]), '.mp3') then
begin
// Bug fix here. Changing from the original TEdit to the OpenDialog.Files[i] fixed song titles in list.
LoadIt := ID3v2Tag.LoadFromFile(OpenDialog1.Files[i]);
AddFileToTree(ListView1, OpenDialog1.Files[i]);
end;
end;
end.
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 340
ClientWidth = 906
Color = clBtnFace
Font.Charset = DEFAULT CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OnCreate = FormCreate
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 906
Height = 97
Align = alTop
TabOrder = 0
ExplicitWidth = 902
object Label1: TLabel
Left = 8
Top = 40
Width = 38
Height = 19
Caption = 'Artist'
Font.Charset = DEFAULT CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
end
object Label2: TLabel
Left = 8
Top = 69
Width = 31
Height = 19
Caption = 'Title'
Font.Charset = DEFAULT CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
end
object Button8: TButton
Left = 16
Top = 11
Width = 59
Height = 22
Caption = 'Select...'
Font.Charset = DEFAULT CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
TabOrder = 0
OnClick = Button8Click
end
object Edit1: TEdit
Left = 81
Top = 11
Width = 265
Height = 21
Font.Charset = DEFAULT CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
TabOrder = 1
end
object strTitle: TEdit
Left = 52
Top = 66
Width = 121
Height = 21
TabOrder = 2
Text = 'Song Title'
end
object strPath: TEdit
Left = 760
Top = 11
Width = 121
Height = 21
TabOrder = 3
Text = 'strPath'
Visible = False
end
object strArtist: TEdit
Left = 52
Top = 39
Width = 121
Height = 21
TabOrder = 4
Text = 'Artist Name'
end
end
object Panel2: TPanel
Left = 81
Top = 131
Width = 785
Height = 201
TabOrder = 1
object ListView1: TListView
Left = 24
Top = 16
Width = 753
Height = 150
Columns = <
item
Caption = 'FileName'
Width = 200
end
item
Caption = 'Title'
Width = 200
end
item
Caption = 'File Location'
Width = 500
end>
MultiSelect = True
RowSelect = True
SortType = stText
TabOrder = 0
ViewStyle = vsReport
OnSelectItem = ListView1SelectItem
end
end
object OpenDialog1: TOpenDialog
Filter = 'MP3|*.mp3'
Options = [ofHideReadOnly, ofAllowMultiSelect, ofEnableSizing]
Left = 390
Top = 12
end
end
program Project1;
uses
Forms,
LoadIt in 'LoadIt.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.