PlayList.pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:9k
- unit PlayList;
- interface
- uses Windows, Classes, Messages, SysUtils, Forms, SyncObjs, WHPlaySDK, Math, ComCtrls, StrUtils,
- ActiveX;
- Const
- WM_ADDITEM=WM_USER+1008;
- Type
- TPlayType=(ptAdd,ptPlay);
- TFileType=(ftUnknown,ftLock,ftUnLock);
- PPlayFileInfo=^TPlayFileInfo;
- TPlayFileInfo=record
- FileName:String;
- DisplayName:String;
- FileSize:Single;
- MediaInfo:TMediaInfo;
- bExists:Boolean;
- bLock:Boolean;
- FileType:TFileType;
- PlayType:TPlayType;
- Pos:Integer;
- end;
- TOnReadyEnd=procedure (Sender:TObject; PlayInfo:PPlayFileInfo; Item:TListItem) of Object;
- TPlayListManager=Class
- private
- FFileName:String;
- FOnReadyEnd: TOnReadyEnd;
- PlayInfoThread:TThread;
- List:TList;
- procedure SetOnReadyEnd(const Value: TOnReadyEnd);
- protected
- ListView:TListView;
- Lock:TCriticalSection;
- Event:TSimpleEvent;
- function GetPlayFileInfo:PPlayFileInfo;
- procedure Add(Info:PPlayFileInfo);overload;
- public
- Constructor Create(ListView:TListView; MsgHandle:THandle);
- Destructor Destroy;override;
- procedure Open(FileName:String; IsAppend:Boolean=true; IsPlay:Boolean=false);
- procedure Save(FileName:String; CurData:TListItem);
- procedure SaveAs(FileName:String);
- procedure Add(FileName:String; FileType:TFileType; PlayType:TPlayType; Pos:Integer=0);overload;
- procedure Delete(Item: TListItem);
- procedure Clear;
- property OnReadyEnd:TOnReadyEnd read FOnReadyEnd write SetOnReadyEnd;
- property FileName:String read FFileName;
- end;
- procedure GetTimeString(iHours,iMinutes,iSeconds:Integer; var Hours,Minutes,Seconds:String);
- implementation
- procedure GetTimeString(iHours,iMinutes,iSeconds:Integer; var Hours,Minutes,Seconds:String);
- begin
- if iHours=0 then
- Hours:='00'
- else
- begin
- Hours:=IntToStr(iHours);
- if Length(Hours)=1 then
- Hours:='0'+Hours;
- end;
- if iMinutes=0 then
- Minutes:='00'
- else
- begin
- Minutes:=IntToStr(iMinutes);
- if Length(Minutes)=1 then
- Minutes:='0'+Minutes;
- end;
- if iSeconds=0 then
- Seconds:='00'
- else
- begin
- Seconds:=IntToStr(iSeconds);
- if Length(Seconds)=1 then
- Seconds:='0'+Seconds;
- end;
- end;
- { TPlayListManager }
- Type
- TPlayInfoThread=Class(TThread)
- private
- MsgHandle:THandle;
- Info:PPlayFileInfo;
- Owner:TPlayListManager;
- procedure DoUpdate;
- protected
- procedure Execute;override;
- public
- Constructor Create(Owner:TPlayListManager; MsgHandle:THandle);
- end;
- procedure TPlayListManager.Add(FileName: String; FileType:TFileType; PlayType:TPlayType; Pos:Integer);
- Var
- Info:PPlayFileInfo;
- begin
- Lock.Acquire;
- Try
- New(Info);
- Info.FileName:=FileName;
- Info.FileType:=FileType;
- Info.PlayType:=PlayType;
- Info.Pos:=Pos;
- List.Add(Info);
- Event.SetEvent;
- Finally
- Lock.Release;
- end;
- end;
- procedure TPlayListManager.Add(Info: PPlayFileInfo);
- begin
- Lock.Acquire;
- Try
- Event.SetEvent;
- List.Add(Info);
- Finally
- Lock.Release;
- end;
- end;
- procedure TPlayListManager.Clear;
- Var
- i:Integer;
- begin
- Lock.Acquire;
- Try
- For i:=0 to List.Count-1 do
- begin
- Dispose(PPlayFileInfo(List.Items[i]));
- end;
- List.Clear;
- Finally
- Lock.Release;
- end;
- end;
- constructor TPlayListManager.Create(ListView:TListView; MsgHandle:THandle);
- begin
- Self.ListView:=ListView;
- Event:=TSimpleEvent.Create;
- Lock:=TCriticalSection.Create;
- PlayInfoThread:=TPlayInfoThread.Create(Self,MsgHandle);
- List:=TList.Create;
- end;
- procedure TPlayListManager.Delete(Item:TListItem);
- begin
- Dispose(PPlayFileInfo(Item.Data));
- Item.Delete;
- end;
- destructor TPlayListManager.Destroy;
- begin
- Clear;
- PlayInfoThread.Terminate;
- Event.SetEvent;
- PlayInfoThread.WaitFor;
- PlayInfoThread.Free;
- Event.Free;
- Lock.Free;
- List.Free;
- inherited;
- end;
- function TPlayListManager.GetPlayFileInfo: PPlayFileInfo;
- begin
- Result:=nil;
- if List.Count>0 then
- begin
- Result:=List.Items[0];
- List.Delete(0);
- end
- else
- Event.ResetEvent;
- end;
- procedure TPlayListManager.Open(FileName: String; IsAppend: Boolean; IsPlay:Boolean);
- Var
- i,Sel:Integer;
- Info:PPlayFileInfo;
- StrList:TStringList;
- ValList:TStringList;
- begin
- StrList:=TStringList.Create;
- ValList:=TStringList.Create;
- Try
- StrList.LoadFromFile(FileName);
- if StrList.Count=0 then
- Exit;
- if Not IsAppend then
- FFileName:=FileName;
- if StrList.Strings[0]='!@#$%^&*(WH)' then
- Sel:=StrToInt(StrList.Strings[1])
- else
- Sel:=0;
- For i:=2 to StrList.Count-1 do
- begin
- New(Info);
- Info.FileName:='';
- Info.DisplayName:='';
- if IsPlay and (i=Sel) then
- Info.PlayType:=ptPlay
- else
- Info.PlayType:=ptAdd;
- Info.Pos:=0;
- Info.FileType:=ftUnknown;
- Info.bLock:=IsLockFile(Info.FileName);
- if StrList.Strings[0]='!@#$%^&*(WH)' then
- begin
- ValList.Text:=AnsiReplaceStr(StrList.Strings[i],',',#13#10);
- Info.FileName:=ValList.Strings[0];
- Info.DisplayName:=ValList.Strings[1];
- end
- else
- Info.FileName:=StrList.Strings[i];
- Self.Add(Info);
- end;
- Finally
- ValList.Free;
- StrList.Free;
- end;
- end;
- procedure TPlayListManager.Save(FileName: String; CurData:TListItem);
- Var
- i:Integer;
- Info:PPlayFileInfo;
- StrList:TStringList;
- begin
- StrList:=TStringList.Create;
- Try
- FFileName:=FileName;
- StrList.Add('!@#$%^&*(WH)');
- StrList.Add('0');
- For i:=0 to ListView.Items.Count-1 do
- begin
- if ListView.Items[i]=CurData then
- begin
- StrList.Strings[1]:=IntToStr(i+2);
- end;
- Info:=ListView.Items[i].Data;
- StrList.Add(Info.FileName+','+Info.DisplayName);
- end;
- StrList.SaveToFile(FileName);
- Finally
- StrList.Free;
- end;
- end;
- procedure TPlayListManager.SaveAs(FileName: String);
- Var
- i:Integer;
- Info:PPlayFileInfo;
- StrList:TStringList;
- begin
- StrList:=TStringList.Create;
- Try
- For i:=0 to ListView.Items.Count-1 do
- begin
- Info:=ListView.Items[i].Data;
- StrList.Add(Info.FileName);
- end;
- StrList.SaveToFile(FileName);
- Finally
- StrList.Free;
- end;
- end;
- procedure TPlayListManager.SetOnReadyEnd(const Value: TOnReadyEnd);
- begin
- FOnReadyEnd := Value;
- end;
- { TPlayInfoThread }
- constructor TPlayInfoThread.Create(Owner:TPlayListManager; MsgHandle:THandle);
- begin
- Inherited Create(true);
- Self.Owner:=Owner;
- Self.FreeOnTerminate:=false;
- Self.MsgHandle:=MsgHandle;
- Self.Resume;
- end;
- procedure TPlayInfoThread.DoUpdate;
- begin
- PostMessage(MsgHandle,WM_ADDITEM,Integer(Info),0);
- end;
- procedure TPlayInfoThread.Execute;
- Var
- FileStream:TFileStream;
- vInfo:PPlayFileInfo;
- begin
- ActiveX.CoInitialize(nil);
- Try
- While Not (Application.Terminated or Terminated) do
- begin
- Owner.Event.WaitFor(INFINITE);
- if Application.Terminated or Terminated then
- Exit;
- Info:=nil;
- Owner.Lock.Acquire;
- Try
- vInfo:=Owner.GetPlayFileInfo;
- if Assigned(vInfo) then
- begin
- New(Info);
- Info^:=vInfo^;
- Dispose(vInfo);
- end;
- Finally
- Owner.Lock.Release;
- end;
- if Not Assigned(Info) then
- Continue;
- Info.bExists:=FileExists(Info.FileName);
- Case Info.FileType of
- ftUnKnown:Info.bLock:=IsLockFile(Info.FileName);
- ftLock:Info.bLock:=true;
- ftUnLock:Info.bLock:=false;
- end;
- FillChar(Info.MediaInfo,sizeof(Info.MediaInfo),0);
- if Info.bLock then
- // dllGetLockMediaInfo(PChar(Info.FileName),@Info.MediaInfo)
- else
- dllGetNoLockMediaInfo(PChar(Info.FileName),@Info.MediaInfo);
- FileStream:=TFileStream.Create(Info.FileName,fmOpenRead or fmShareDenyNone);
- Try
- Info.FileSize:=RoundTo(FileStream.Size div (1024*1024),-2);
- Finally
- FileStream.Free;
- FileStream:=nil;
- end;
- if Info.DisplayName='' then
- Info.DisplayName:=ChangeFileExt(ExtractFileName(Info.FileName),'');
- Info.MediaInfo.PlayHours:=Info.MediaInfo.PlayMinites div 60;
- Info.MediaInfo.PlayMinites:=Info.MediaInfo.PlayMinites mod 60;
- DoUpdate;
- end;
- Finally
- ActiveX.CoUninitialize;
- end;
- end;
- end.