PlayList.~pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:9k
源码类别:

DirextX编程

开发平台:

Delphi

  1. unit PlayList;
  2. interface
  3. uses Windows, Classes, Messages, SysUtils, Forms, SyncObjs, WHPlaySDK, Math, ComCtrls, StrUtils,
  4.     ActiveX;
  5. Const
  6.   WM_ADDITEM=WM_USER+1008;
  7. Type
  8.     TPlayType=(ptAdd,ptPlay);
  9.     TFileType=(ftUnknown,ftLock,ftUnLock);
  10.     PPlayFileInfo=^TPlayFileInfo;
  11.     TPlayFileInfo=record
  12.         FileName:String;
  13.         DisplayName:String;
  14.         FileSize:Single;
  15.         MediaInfo:TMediaInfo;
  16.         bExists:Boolean;
  17.         bLock:Boolean;
  18.         FileType:TFileType;
  19.         PlayType:TPlayType;
  20.         Pos:Integer;
  21.     end;
  22.     TOnReadyEnd=procedure (Sender:TObject; PlayInfo:PPlayFileInfo; Item:TListItem) of Object;
  23.     TPlayListManager=Class
  24.     private
  25.         FFileName:String;
  26.         FOnReadyEnd: TOnReadyEnd;
  27.         PlayInfoThread:TThread;
  28.         List:TList;
  29.         procedure SetOnReadyEnd(const Value: TOnReadyEnd);
  30.     protected
  31.         ListView:TListView;
  32.         Lock:TCriticalSection;
  33.         Event:TSimpleEvent;
  34.         function GetPlayFileInfo:PPlayFileInfo;
  35.         procedure Add(Info:PPlayFileInfo);overload;
  36.     public
  37.         Constructor Create(ListView:TListView; MsgHandle:THandle);
  38.         Destructor Destroy;override;
  39.         procedure Open(FileName:String; IsAppend:Boolean=true; IsPlay:Boolean=false);
  40.         procedure Save(FileName:String; CurData:TListItem);
  41.         procedure SaveAs(FileName:String);
  42.         procedure Add(FileName:String; FileType:TFileType; PlayType:TPlayType; Pos:Integer=0);overload;
  43.         procedure Delete(Item: TListItem);
  44.         procedure Clear;
  45.         property OnReadyEnd:TOnReadyEnd read FOnReadyEnd write SetOnReadyEnd;
  46.         property FileName:String read FFileName;
  47.     end;
  48. procedure GetTimeString(iHours,iMinutes,iSeconds:Integer; var Hours,Minutes,Seconds:String);
  49. implementation
  50. procedure GetTimeString(iHours,iMinutes,iSeconds:Integer; var Hours,Minutes,Seconds:String);
  51. begin
  52.     if iHours=0 then
  53.         Hours:='00'
  54.     else
  55.     begin
  56.         Hours:=IntToStr(iHours);
  57.         if Length(Hours)=1 then
  58.              Hours:='0'+Hours;
  59.     end;
  60.     if iMinutes=0 then
  61.         Minutes:='00'
  62.     else
  63.     begin
  64.         Minutes:=IntToStr(iMinutes);
  65.         if Length(Minutes)=1 then
  66.              Minutes:='0'+Minutes;
  67.     end;
  68.     if iSeconds=0 then
  69.         Seconds:='00'
  70.     else
  71.     begin
  72.         Seconds:=IntToStr(iSeconds);
  73.         if Length(Seconds)=1 then
  74.              Seconds:='0'+Seconds;
  75.     end;
  76. end;
  77. { TPlayListManager }
  78. Type
  79.     TPlayInfoThread=Class(TThread)
  80.     private
  81.         MsgHandle:THandle;
  82.         Info:PPlayFileInfo;
  83.         Owner:TPlayListManager;
  84.         procedure DoUpdate;
  85.     protected
  86.         procedure Execute;override;
  87.     public
  88.         Constructor Create(Owner:TPlayListManager; MsgHandle:THandle);
  89.     end;
  90. procedure TPlayListManager.Add(FileName: String; FileType:TFileType; PlayType:TPlayType; Pos:Integer);
  91. Var
  92.     Info:PPlayFileInfo;
  93. begin
  94.     Lock.Acquire;
  95.     Try
  96.         New(Info);
  97.         Info.FileName:=FileName;
  98.         Info.FileType:=FileType;
  99.         Info.PlayType:=PlayType;
  100.         Info.Pos:=Pos;
  101.         List.Add(Info);
  102.         Event.SetEvent;
  103.     Finally
  104.         Lock.Release;
  105.     end;
  106. end;
  107. procedure TPlayListManager.Add(Info: PPlayFileInfo);
  108. begin
  109.     Lock.Acquire;
  110.     Try
  111.         Event.SetEvent;
  112.         List.Add(Info);
  113.     Finally
  114.         Lock.Release;
  115.     end;
  116. end;
  117. procedure TPlayListManager.Clear;
  118. Var
  119.     i:Integer;
  120. begin
  121.     Lock.Acquire;
  122.     Try
  123.         For i:=0 to List.Count-1 do
  124.         begin
  125.             Dispose(PPlayFileInfo(List.Items[i]));
  126.         end;
  127.         List.Clear;
  128.     Finally
  129.         Lock.Release;
  130.     end;
  131. end;
  132. constructor TPlayListManager.Create(ListView:TListView; MsgHandle:THandle);
  133. begin
  134.     Self.ListView:=ListView;
  135.     Event:=TSimpleEvent.Create;
  136.     Lock:=TCriticalSection.Create;
  137.     PlayInfoThread:=TPlayInfoThread.Create(Self,MsgHandle);
  138.     List:=TList.Create;
  139. end;
  140. procedure TPlayListManager.Delete(Item:TListItem);
  141. begin
  142.     Dispose(PPlayFileInfo(Item.Data));
  143.     Item.Delete;
  144. end;
  145. destructor TPlayListManager.Destroy;
  146. begin
  147.     Clear;
  148.     PlayInfoThread.Terminate;
  149.     Event.SetEvent;
  150.     PlayInfoThread.WaitFor;
  151.     PlayInfoThread.Free;
  152.     Event.Free;
  153.     Lock.Free;
  154.     List.Free;
  155.     inherited;
  156. end;
  157. function TPlayListManager.GetPlayFileInfo: PPlayFileInfo;
  158. begin
  159.     Result:=nil;
  160.     if List.Count>0 then
  161.     begin
  162.         Result:=List.Items[0];
  163.         List.Delete(0);
  164.     end
  165.     else
  166.         Event.ResetEvent;
  167. end;
  168. procedure TPlayListManager.Open(FileName: String; IsAppend: Boolean; IsPlay:Boolean);
  169. Var
  170.     i,Sel:Integer;
  171.     Info:PPlayFileInfo;
  172.     StrList:TStringList;
  173.     ValList:TStringList;
  174. begin
  175.     StrList:=TStringList.Create;
  176.     ValList:=TStringList.Create;
  177.     Try
  178.         StrList.LoadFromFile(FileName);
  179.         if StrList.Count=0 then
  180.             Exit;
  181.         if Not IsAppend then
  182.             FFileName:=FileName;
  183.         if StrList.Strings[0]='!@#$%^&*(WH)' then
  184.             Sel:=StrToInt(StrList.Strings[1])
  185.         else
  186.             Sel:=0;
  187.         For i:=2 to StrList.Count-1 do
  188.         begin
  189.             New(Info);
  190.             Info.FileName:='';
  191.             Info.DisplayName:='';
  192.             if IsPlay and (i=Sel) then
  193.                 Info.PlayType:=ptPlay
  194.             else
  195.                 Info.PlayType:=ptAdd;
  196.             Info.Pos:=0;
  197.             if StrList.Strings[0]='!@#$%^&*(WH)' then
  198.             begin
  199.                 ValList.Text:=AnsiReplaceStr(StrList.Strings[i],',',#13#10);
  200.                 Info.FileName:=ValList.Strings[0];
  201.                 Info.DisplayName:=ValList.Strings[1];
  202.             end
  203.             else
  204.                 Info.FileName:=StrList.Strings[i];
  205.             Self.Add(Info);
  206.         end;
  207.     Finally
  208.         ValList.Free;
  209.         StrList.Free;
  210.     end;
  211. end;
  212. procedure TPlayListManager.Save(FileName: String; CurData:TListItem);
  213. Var
  214.     i:Integer;
  215.     Info:PPlayFileInfo;
  216.     StrList:TStringList;
  217. begin
  218.     StrList:=TStringList.Create;
  219.     Try
  220.         FFileName:=FileName;
  221.         StrList.Add('!@#$%^&*(WH)');
  222.         StrList.Add('0');
  223.         For i:=0 to ListView.Items.Count-1 do
  224.         begin
  225.             if ListView.Items[i]=CurData then
  226.             begin
  227.                 StrList.Strings[1]:=IntToStr(i+2);
  228.             end;
  229.             Info:=ListView.Items[i].Data;
  230.             StrList.Add(Info.FileName+','+Info.DisplayName);
  231.         end;
  232.         StrList.SaveToFile(FileName); 
  233.     Finally
  234.         StrList.Free;
  235.     end;
  236. end;
  237. procedure TPlayListManager.SaveAs(FileName: String);
  238. Var
  239.     i:Integer;
  240.     Info:PPlayFileInfo;
  241.     StrList:TStringList;
  242. begin
  243.     StrList:=TStringList.Create;
  244.     Try
  245.         For i:=0 to ListView.Items.Count-1 do
  246.         begin
  247.             Info:=ListView.Items[i].Data;
  248.             StrList.Add(Info.FileName);
  249.         end;
  250.         StrList.SaveToFile(FileName);
  251.     Finally
  252.         StrList.Free;
  253.     end;
  254. end;
  255. procedure TPlayListManager.SetOnReadyEnd(const Value: TOnReadyEnd);
  256. begin
  257.     FOnReadyEnd := Value;
  258. end;
  259. { TPlayInfoThread }
  260. constructor TPlayInfoThread.Create(Owner:TPlayListManager; MsgHandle:THandle);
  261. begin
  262.     Inherited Create(true);
  263.     Self.Owner:=Owner;
  264.     Self.FreeOnTerminate:=false;
  265.     Self.MsgHandle:=MsgHandle;
  266.     Self.Resume;
  267. end;
  268. procedure TPlayInfoThread.DoUpdate;
  269. begin
  270.     PostMessage(MsgHandle,WM_ADDITEM,Integer(Info),0);
  271. end;
  272. procedure TPlayInfoThread.Execute;
  273. Var
  274.     FileStream:TFileStream;
  275.     vInfo:PPlayFileInfo;
  276. begin
  277.     ActiveX.CoInitialize(nil);
  278.     Try
  279.         While Not (Application.Terminated or Terminated) do
  280.         begin
  281.             Owner.Event.WaitFor(INFINITE);
  282.             if Application.Terminated or Terminated then
  283.                 Exit;
  284.             Info:=nil;
  285.             Owner.Lock.Acquire;
  286.             Try
  287.                 vInfo:=Owner.GetPlayFileInfo;
  288.                 if Assigned(vInfo) then
  289.                 begin
  290.                     New(Info);
  291.                     Info^:=vInfo^;
  292.                     Dispose(vInfo);
  293.                 end;
  294.             Finally
  295.                 Owner.Lock.Release;
  296.             end;
  297.             if Not Assigned(Info) then
  298.                 Continue;
  299.             Info.bExists:=FileExists(Info.FileName);
  300.             Case Info.FileType of
  301.                 ftUnKnown:Info.bLock:=IsLockFile(Info.FileName);
  302.                 ftLock:Info.bLock:=true;
  303.                 ftUnLock:Info.bLock:=false;
  304.             end;
  305.             FillChar(Info.MediaInfo,sizeof(Info.MediaInfo),0);
  306.             if Info.bLock then
  307.             //    dllGetLockMediaInfo(PChar(Info.FileName),@Info.MediaInfo)
  308.             else
  309.                 dllGetNoLockMediaInfo(PChar(Info.FileName),@Info.MediaInfo);
  310.             FileStream:=TFileStream.Create(Info.FileName,fmOpenRead or fmShareDenyNone);
  311.             Try
  312.                 Info.FileSize:=RoundTo(FileStream.Size div (1024*1024),-2);
  313.             Finally
  314.                 FileStream.Free;
  315.                 FileStream:=nil;
  316.             end;
  317.             if Info.DisplayName='' then
  318.                 Info.DisplayName:=ChangeFileExt(ExtractFileName(Info.FileName),'');
  319.             Info.MediaInfo.PlayHours:=Info.MediaInfo.PlayMinites div 60;
  320.             Info.MediaInfo.PlayMinites:=Info.MediaInfo.PlayMinites mod 60;
  321.             DoUpdate;
  322.         end;
  323.     Finally
  324.         ActiveX.CoUninitialize;
  325.     end;
  326. end;
  327. end.