Unit1.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:11k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DSPack,
  5.   Menus, ExtCtrls, ComCtrls, StdCtrls, DirectShow9, OleServer, DSUtil,
  6.   ImgList, ToolWin, shellapi;
  7. type
  8.   TFormDVDPlayer = class(TForm)
  9.     MainMenu: TMainMenu;
  10.     FileMenu: TMenuItem;
  11.     OpenMenu: TMenuItem;
  12.     ExitMenu: TMenuItem;
  13.     OpenDialog: TOpenDialog;
  14.     ControlMenu: TMenuItem;
  15.     PlayMenu: TMenuItem;
  16.     PauseMenu: TMenuItem;
  17.     StopMenu: TMenuItem;
  18.     ToolBar: TToolBar;
  19.     btPreviousChapter: TToolButton;
  20.     btRewind: TToolButton;
  21.     btPause: TToolButton;
  22.     btPlay: TToolButton;
  23.     btStop: TToolButton;
  24.     btFastForward: TToolButton;
  25.     btNextChapter: TToolButton;
  26.     ToolButton8: TToolButton;
  27.     btRootMenu: TToolButton;
  28.     btFullScreen: TToolButton;
  29.     btFrameStep: TToolButton;
  30.     ImageList: TImageList;
  31.     StepForwardMenu: TMenuItem;
  32.     OptionsMenu: TMenuItem;
  33.     FullscreenMenu: TMenuItem;
  34.     Popup: TPopupMenu;
  35.     N1: TMenuItem;
  36.     GoToMenu: TMenuItem;
  37.     PlaySpeed1: TMenuItem;
  38.     NextChapterMenu: TMenuItem;
  39.     PreviousChapterMenu: TMenuItem;
  40.     FastForward1: TMenuItem;
  41.     Rewind1: TMenuItem;
  42.     N4: TMenuItem;
  43.     MenuRoot: TMenuItem;
  44.     TitleMenu: TMenuItem;
  45.     N5: TMenuItem;
  46.     BookmarksMenu: TMenuItem;
  47.     SavebookmarkMenu: TMenuItem;
  48.     RestorebookmarkMenu: TMenuItem;
  49.     Play2: TMenuItem;
  50.     Pause2: TMenuItem;
  51.     Stop2: TMenuItem;
  52.     N2: TMenuItem;
  53.     GoTo2: TMenuItem;
  54.     NextChapter2: TMenuItem;
  55.     PreviousChapter2: TMenuItem;
  56.     PlaySpeed2: TMenuItem;
  57.     FastForward2: TMenuItem;
  58.     Rewind2: TMenuItem;
  59.     N3: TMenuItem;
  60.     MenuRoot2: TMenuItem;
  61.     TitleMenu2: TMenuItem;
  62.     Fullscreen2: TMenuItem;
  63.     N6: TMenuItem;
  64.     Bookmarks2: TMenuItem;
  65.     Savebookmark2: TMenuItem;
  66.     Restorebookmark2: TMenuItem;
  67.     StatusBar: TStatusBar;
  68.     N7: TMenuItem;
  69.     progdigycom1: TMenuItem;
  70.     VideoWindow: TVideoWindow;
  71.     FilterGraph: TFilterGraph;
  72.     procedure PlayMenuClick(Sender: TObject);
  73.     procedure btStopClick(Sender: TObject);
  74.     procedure btPauseClick(Sender: TObject);
  75.     procedure btFullScreenClick(Sender: TObject);
  76.     procedure btFrameStepClick(Sender: TObject);
  77.     procedure btRootMenuClick(Sender: TObject);
  78.     procedure VideoWindowMouseMove(Sender: TObject; Shift: TShiftState; X,
  79.       Y: Integer);
  80.     procedure VideoWindowMouseUp(Sender: TObject; Button: TMouseButton;
  81.       Shift: TShiftState; X, Y: Integer);
  82.     procedure btNextChapterClick(Sender: TObject);
  83.     procedure btPreviousChapterClick(Sender: TObject);
  84.     procedure btFastForwardClick(Sender: TObject);
  85.     procedure btRewindClick(Sender: TObject);
  86.     procedure TitleMenuClick(Sender: TObject);
  87.     procedure SavebookmarkMenuClick(Sender: TObject);
  88.     procedure RestorebookmarkMenuClick(Sender: TObject);
  89.     procedure ExitMenuClick(Sender: TObject);
  90.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  91.     procedure OpenMenuClick(Sender: TObject);
  92.     procedure FilterGraphDVDTitleChange(sender: TObject; title: Integer);
  93.     procedure FilterGraphDVDChapterStart(sender: TObject;
  94.       chapter: Integer);
  95.     procedure FilterGraphDVDCurrentHMSFTime(sender: TObject;
  96.       HMSFTimeCode: tagDVD_HMSF_TIMECODE; TimeCode: tagDVD_TIMECODE);
  97.   private
  98.     { D閏larations priv閑s }
  99.   public
  100.     { D閏larations publiques }
  101.     procedure PlayDVD(FileName: WideString);
  102.   end;
  103. var
  104.   FormDVDPlayer: TFormDVDPlayer;
  105. implementation
  106. {$R *.DFM}
  107. procedure TFormDVDPlayer.PlayDVD(FileName: WideString);
  108. var
  109.   Status : TAMDVDRenderStatus;
  110.   DvdCmd: IDvdCmd;
  111.   HR: HRESULT;
  112.   DVDControl: IDVDControl2;
  113. begin
  114.   // Activate the filter Graph
  115.   if not FilterGraph.Active then
  116.   begin
  117.     FilterGraph.Active := true;
  118.     // Render DVD
  119.     HR := FilterGraph.RenderDvd(Status, FileName);
  120.     if HR <> S_OK then
  121.     begin
  122.       case HR of
  123.         HRESULT(E_INVALIDARG): Application.MessageBox('Invalid Argument.','Error', mb_ok);
  124.         HRESULT(S_FALSE)     : begin
  125.                         if Status.hrVPEStatus <> 0 then Application.MessageBox(PChar(GetErrorString(Status.hrVPEStatus)), 'Error', mb_OK);
  126.                         if Status.bDvdVolInvalid   then Application.MessageBox('The specified DVD volume to be played does not exist.', 'Error', mb_OK);
  127.                         if Status.bDvdVolUnknown   then Application.MessageBox('No DVD volume is specified or isn''t found.', 'Error', mb_OK);
  128.                         if Status.bNoLine21In      then Application.MessageBox('The video decoder doesn''t produce line 21 (closed captioning) data.', 'Error', mb_OK);
  129.                         if Status.bNoLine21Out     then Application.MessageBox('The video decoder can''t be shown as closed captioning on video due to a problem with graph building.', 'Error', mb_OK);
  130.                         if status.iNumStreamsFailed > 0 then Application.MessageBox('Can''t render one or more stream.', 'Error', mb_OK);
  131.                       end;
  132.         HRESULT(VFW_E_DVD_DECNOTENOUGH) : Application.MessageBox('There isn''t enough hardware or software decoders to decode all streams.','Error', mb_OK);
  133.         HRESULT(VFW_E_DVD_RENDERFAIL)   : Application.MessageBox('Some basic error occurred in building the graph.'#13'Possibilities include the DVD Navigator filter or the video or audio renderer not instantiating,'#13'a trivial connection or pin enumeration failing, or none of the streams rendering.','Error', mb_OK);
  134.       end;
  135.       FilterGraph.ClearGraph;
  136.       exit;
  137.     end;
  138.     FilterGraph.Play;
  139.     FilterGraph.QueryInterface(IDVDControl2, DVDControl);
  140.     DVDControl.SetOption(DVD_NotifyParentalLevelChange,false); //not notify us when parental level changes
  141.     DVDControl.SetOption(DVD_HMSF_TimeCodeEvents, true);       // use new HMSF timecode format
  142.   end
  143.   else
  144.   begin
  145.     FilterGraph.Play;
  146.     FilterGraph.QueryInterface(IDVDControl2, DVDControl);
  147.     DVDControl.PlayForwards(1.0,DVD_CMD_FLAG_None, DvdCmd);
  148.   end;
  149.   DVDControl := nil;
  150. end;
  151. procedure TFormDVDPlayer.PlayMenuClick(Sender: TObject);
  152. begin
  153.   PlayDVD('');
  154. end;
  155. procedure TFormDVDPlayer.btStopClick(Sender: TObject);
  156. begin
  157.   FilterGraph.Stop;
  158. end;
  159. procedure TFormDVDPlayer.btPauseClick(Sender: TObject);
  160. begin
  161.   FilterGraph.Pause;
  162. end;
  163. procedure TFormDVDPlayer.btFullScreenClick(Sender: TObject);
  164. begin
  165.   VideoWindow.FullScreen := not VideoWindow.FullScreen;
  166.   btFullScreen.Down := VideoWindow.FullScreen;
  167. end;
  168. procedure TFormDVDPlayer.btFrameStepClick(Sender: TObject);
  169. var
  170.   FrameStep: IVideoFrameStep;
  171. begin
  172.   if FilterGraph.Active then
  173.   begin
  174.     FilterGraph.QueryInterface(IVideoFrameStep, FrameStep);
  175.     FrameStep.Step(1,nil);
  176.     FrameStep := nil;
  177.   end;
  178. end;
  179. procedure TFormDVDPlayer.btRootMenuClick(Sender: TObject);
  180. var
  181.   DvdCmd: IDvdCmd;
  182.   DvdControl: IDvdControl2;
  183. begin
  184.   if FilterGraph.Active then
  185.   begin
  186.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  187.     DvdControl.ShowMenu(DVD_MENU_Root, 0, DvdCmd);
  188.     DvdControl := nil;
  189.   end;
  190. end;
  191. procedure TFormDVDPlayer.VideoWindowMouseMove(Sender: TObject;
  192.   Shift: TShiftState; X, Y: Integer);
  193. var
  194.   Point : TPoint;
  195.   DvdControl: IDvdControl2;
  196. begin
  197.   if FilterGraph.Active then
  198.   begin
  199.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  200.     Point.x := x;
  201.     Point.y := y;
  202.     DvdControl.SelectAtPosition(Point);
  203.     DvdControl := nil;
  204.   end;
  205. end;
  206. procedure TFormDVDPlayer.VideoWindowMouseUp(Sender: TObject;
  207.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  208. var
  209.   Point : TPoint;
  210.   DvdControl: IDvdControl2;
  211. begin
  212.   if FilterGraph.Active then
  213.   begin
  214.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  215.     Point.x := x;
  216.     Point.y := y;
  217.     DvdControl.ActivateAtPosition(Point);
  218.     DvdControl := nil;
  219.   end;
  220. end;
  221. procedure TFormDVDPlayer.btNextChapterClick(Sender: TObject);
  222. var
  223.   DvdCmd: IDvdCmd;
  224.   DvdControl: IDvdControl2;
  225. begin
  226.   if FilterGraph.Active then
  227.   begin
  228.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  229.     DvdControl.PlayNextChapter(DVD_CMD_FLAG_None, DvdCmd);
  230.     DvdControl := nil;
  231.   end;
  232. end;
  233. procedure TFormDVDPlayer.btPreviousChapterClick(Sender: TObject);
  234. var
  235.   DvdCmd: IDvdCmd;
  236.   DvdControl: IDvdControl2;
  237. begin
  238.   if FilterGraph.Active then
  239.   begin
  240.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  241.     DvdControl.PlayPrevChapter(DVD_CMD_FLAG_None, DvdCmd);
  242.     DvdControl := nil;
  243.   end;
  244. end;
  245. procedure TFormDVDPlayer.btFastForwardClick(Sender: TObject);
  246. var
  247.   DvdCmd: IDvdCmd;
  248.   DvdControl: IDvdControl2;
  249. begin
  250.   if FilterGraph.Active then
  251.   begin
  252.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  253.     DvdControl.PlayForwards(8.0,DVD_CMD_FLAG_None, DvdCmd);
  254.     DvdControl := nil;
  255.   end;
  256. end;
  257. procedure TFormDVDPlayer.btRewindClick(Sender: TObject);
  258. var
  259.   DvdCmd: IDvdCmd;
  260.   DvdControl: IDvdControl2;
  261. begin
  262.   if FilterGraph.Active then
  263.   begin
  264.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  265.     DvdControl.PlayBackwards(8.0,DVD_CMD_FLAG_None, DvdCmd);
  266.     DvdControl := nil;
  267.   end;
  268. end;
  269. procedure TFormDVDPlayer.TitleMenuClick(Sender: TObject);
  270. var
  271.   DvdCmd: IDvdCmd;
  272.   DvdControl: IDvdControl2;
  273. begin
  274.   if FilterGraph.Active then
  275.   begin
  276.     FilterGraph.QueryInterface(IDvdControl2, DvdControl);
  277.     DvdControl.ShowMenu(DVD_MENU_Title, DVD_CMD_FLAG_None, DvdCmd);
  278.     DvdControl := nil;
  279.   end;
  280. end;
  281. procedure TFormDVDPlayer.SavebookmarkMenuClick(Sender: TObject);
  282. begin
  283.   FilterGraph.DVDSaveBookmark(ExtractFilePath(Application.EXEName)+'bookmark.bmk');
  284. end;
  285. procedure TFormDVDPlayer.RestorebookmarkMenuClick(Sender: TObject);
  286. begin
  287.   FilterGraph.DVDRestoreBookmark(ExtractFilePath(Application.EXEName)+'bookmark.bmk');
  288. end;
  289. procedure TFormDVDPlayer.ExitMenuClick(Sender: TObject);
  290. begin
  291.   FilterGraph.ClearGraph;
  292.   FilterGraph.Active := false;
  293.   Application.Terminate;
  294. end;
  295. procedure TFormDVDPlayer.FormCloseQuery(Sender: TObject;
  296.   var CanClose: Boolean);
  297. begin
  298.   ExitMenuClick(nil)
  299. end;
  300. procedure TFormDVDPlayer.OpenMenuClick(Sender: TObject);
  301. begin
  302.   if OpenDialog.Execute then
  303.   begin
  304.     FilterGraph.ClearGraph;
  305.     FilterGraph.Active := false;
  306.     PlayDVD(OpenDialog.FileName);
  307.   end;
  308.   
  309. end;
  310. procedure TFormDVDPlayer.FilterGraphDVDTitleChange(sender: TObject;
  311.   title: Integer);
  312. begin
  313.   statusbar.Panels.Items[1].Text := 'Title '+inttostr(title);
  314. end;
  315. procedure TFormDVDPlayer.FilterGraphDVDChapterStart(sender: TObject;
  316.   chapter: Integer);
  317. begin
  318.   statusbar.Panels.Items[2].Text := 'Chapter '+ inttostr(chapter);
  319. end;
  320. procedure TFormDVDPlayer.FilterGraphDVDCurrentHMSFTime(sender: TObject;
  321.   HMSFTimeCode: tagDVD_HMSF_TIMECODE; TimeCode: tagDVD_TIMECODE);
  322. begin
  323.   with HMSFTimeCode do
  324.     StatusBar.Panels.Items[0].Text :=
  325.       format('%d:%d:%d',[bHours, bMinutes ,bSeconds]);
  326. end;
  327. end.