我下了,帖
M3UFiles.pas
============================================================
unit M3UFiles;
interface
uses SysUtils, Classes, StrUtils;
type
TM3uItem = record
Time : LongInt;
Title : string;
FilePath : string;
end;
type
EM3uFileException = class(Exception);
TM3uFile = class(TObject)
private
FFileName: string;
FList: TList;
procedure SetList(const Value: TList);
function ReadList:Boolean;
function SaveList:boolean;
procedure SetFileName(const Value: string);
public
constructor Create(const FileName: string);
property FileName: string read FFileName write SetFileName;
property List: TList read FList write SetList;
procedure ClearList;
function OpenPlayListFile(FileName : string): boolean;
function SavePlayListFile(FileName : string): boolean;
procedure Free;
end;
implementation
procedure TM3uFile.ClearList;
var
i :integer;
begin
for i:=0 to FList.Count-1do
begin
Dispose(FList)
end;
FList.Clear;
end;
constructor TM3uFile.Create(const FileName: string);
begin
FList := TList.Create;
FFileName := FileName;
if FFileName = '' then
exit;
ReadList;
end;
procedure TM3uFile.Free;
begin
FList.Free;
end;
function TM3uFile.OpenPlayListFile(FileName: string): boolean;
begin
FFileName := FileName;
Result:=ReadList;
end;
function TM3uFile.ReadList:Boolean;
function GetTimeFrom_sLine(sline_EXTINF : string) : LongInt;
var
timestr : string;
begin
if LeftStr(sline_EXTINF,7) <> '#EXTINF' then
begin
result:=-1;
exit;
end;
try
timestr := Copy(sline_EXTINF,9,Pos(',',sline_EXTINF)-9);
result:=StrToInt(timestr);
except
result:=-1;
end;
end;
function GetTitleFrom_sLine(sline_EXTINF : string) : string;
var
titlestr : string;
begin
if LeftStr(sline_EXTINF,7) <> '#EXTINF' then
begin
result:='';
exit;
end;
try
titlestr := Copy(sline_EXTINF,Pos(',',sline_EXTINF)+1,Length(sline_EXTINF)-Pos(',',sline_EXTINF)+2);
result:=titlestr;
except
result:='';
end;
end;
var
slTmp : TStrings;
NewItem : ^TM3uItem;
i : integer;
begin
result := True;
ClearList;
if FFileName='' then
exit;
slTmp := TStringList.Create;
try
slTmp.LoadFromFile(FFileName);
New(NewItem);
for i := 0 to slTmp.Count - 1do
begin
if slTmp.Strings[1]='#EXTM3U' then
continue;
if slTmp.Strings[1]='#' then
begin
NewItem^.Time:=GetTimeFrom_sLine(slTmp.Strings);
NewItem^.Title:=GetTitleFrom_sLine(slTmp.Strings);
end
else
if slTmp.Strings[1]<>'#' then
begin
NewItem.FilePath := slTmp.Strings;
FList.Add(NewItem);
New(NewItem);
end;
end;
except
result := false;
end;
slTmp.Free;
end;
function TM3uFile.SaveList:Boolean;
var
slTmp : TStrings;
NewItem : ^TM3uItem;
i : integer;
begin
result:=True;
if FFileName='' then
exit;
slTmp := TStringList.Create;
slTmp.Clear;
slTmp.Add('#EXTM3U');
for i:=0 to FList.Count-1do
begin
NewItem := FList;
if (NewItem^.Time<>-1) or (NewItem^.Title<>'') then
begin
slTmp.Add('#EXTINF'+':'+IntToStr(NewItem^.Time)+','+NewItem^.Title);
slTmp.Add(NewItem^.FilePath);
end;
end;
try
slTmp.SaveToFile(FFileName);
except
result := false;
end;
slTmp.Free;
end;
function TM3uFile.SavePlayListFile(FileName: string): boolean;
begin
if FileName <> '' then
FFileName := FileName;
result:=SaveList;
end;
procedure TM3uFile.SetFileName(const Value: string);
begin
FFileName := Value;
ReadList;
end;
procedure TM3uFile.SetList(const Value: TList);
begin
FList := Value;
end;
end.
==========================================================================
使用:
unit tt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, M3UFiles, StdCtrls, StrUtils, ComCtrls;
type
Tm3uForm = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
ListView1: TListView;
Button2: TButton;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
m3uForm: Tm3uForm;
myM3uFile : TM3uFile;
implementation
{$R *.dfm}
procedure Tm3uForm.Button1Click(Sender: TObject);
var
AItem : ^TM3uItem;
i:integer;
begin
if OpenDialog1.Execute then
begin
myM3uFile.OpenPlayListFile(OpenDialog1.FileName);
ListView1.Clear;
for i:=0 to myM3uFile.List.Count-1do
begin
AItem:=myM3uFile.List;
with ListView1.Items.Adddo
begin
Caption:=AItem^.Title;
SubItems.Add(inttostr(AItem^.Time)+' s');
SubItems.Add(AItem^.FilePath);
end;
end;
end;
end;
procedure Tm3uForm.FormCreate(Sender: TObject);
begin
myM3uFile := TM3uFile.Create(GetCurrentDir+'/'+'1.m3u');
end;
procedure Tm3uForm.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
myM3uFile.Free;
end;
procedure Tm3uForm.Button2Click(Sender: TObject);
var
i : integer;
NewItem : ^TM3uItem;
begin
if SaveDialog1.Execute then
begin
myM3uFile.ClearList;
for i:=0 to ListView1.Items.Count-1do
begin
new(NewItem);
NewItem^.Title := ListView1.Items.Caption;
NewItem^.Time := StrToInt(Trim(StringReplace(ListView1.Items.SubItems[0],'s','',[rfReplaceAll])));
NewItem^.FilePath := ListView1.Items.SubItems[1];
myM3uFile.List.Add(NewItem);
end;
myM3uFile.SavePlayListFile(SaveDialog1.FileName);
end;
end;
end.