Mega Code Archive

 
Categories / Delphi / Multimedia
 

Directx kullanarak film-ses denetimi - 1

//DirectX nesneleri/dcu'ları kurulu olmalı //http://www.delphi-jedi.org/DelphiGraphics/jedi-index.htm inceleyiniz //Ses kontrolü, trackbar ile ileri geri alabilme özellikleri var. unit mainunit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Buttons, ActiveX, ComCtrls, DirectShow; Const dsEventMessage = WM_APP+12345; // Unique event identifier dsEventInstance : Integer = 54321; // Unique event identifier dsEventsOn : Integer = 0; dsEventsOff : Integer = 1; VolumeTable : Array[0..7] of Integer = (-10000,-4800,-2400,-1200,-600,-300,-150,0); type TMainForm = class(TForm) ControlPanel: TPanel; VideoPanel: TPanel; PlayButton: TSpeedButton; StopButton: TSpeedButton; QuitButton: TSpeedButton; OpenButton: TSpeedButton; OpenDialog: TOpenDialog; SeekTimer: TTimer; TimeLinePanel: TPanel; TimeLineImage: TImage; VolumeBar: TTrackBar; procedure QuitButtonClick(Sender: TObject); procedure OpenButtonClick(Sender: TObject); procedure StopButtonClick(Sender: TObject); procedure PlayButtonClick(Sender: TObject); procedure FormResize(Sender: TObject); procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure SeekTimerTimer(Sender: TObject); procedure TimeLineImageMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure TimeLineImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure VolumeBarChange(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } procedure dsEventPop(var M : TMessage); message dsEventMessage; public { Public declarations } function CreateMovieInterface : Boolean; procedure DestroyMovieInterface; procedure SetWindowPos(pLeft,pTop,pWidth,pHeight : Integer); procedure ResetWindowPos; procedure SetCurrentPosition(mPos : Int64); function GetCurrentPosition : Int64; procedure UpdateTimeLine; procedure SetVolume(vLevel : Integer); end; Const stClosed = 0; stPlaying = 1; stStopped = 2; stPaused = 3; stOpen = 4; var MainForm : TMainForm; // DirectShow Interfaces dsGraphBuilder : IGraphBuilder = NIL; // Used to open the file and render the filters dsMediaControl : IMediaControl = NIL; // Play, Stop, Pause. dsMediaSeeking : IMediaSeeking = NIL; // Set position. dsBasicAudio : IBasicAudio = NIL; // Volume/Balance control. dsBasicVideo : IBasicVideo2 = NIL; // Video source size dsVideoWindow : IVideoWindow = NIL; // Window positioning dsMediaEventEx : IMediaEventEx = NIL; // Event handling // Program vars plState : Integer = stClosed; // Play State MediaLength : Int64 = -1; // Length of open media MediaPosition : Int64 = 0; // Used to draw the time bar implementation {$R *.dfm} function TMainForm.CreateMovieInterface : Boolean; begin Result := False; // Init COM Interface If failed(CoInitialize(NIL)) then Exit; // Create DirectShow Graph if failed(CoCreateInstance(CLSID_FilterGraph,NIL, CLSCTX_INPROC,IID_IGraphBuilder,dsGraphBuilder)) then exit; // Get the IMediaControl Interface if failed(dsGraphBuilder.QueryInterface(IID_IMediaControl,dsMediaControl)) then exit; // Get the IMediaSeeking Interface if failed(dsGraphBuilder.QueryInterface(IID_IMediaSeeking,dsMediaSeeking)) then exit; // Get the IBasicAudio Interface if failed(dsGraphBuilder.QueryInterface(IID_IBasicAudio,dsBasicAudio)) then exit; // Get the IBasicWindow Interface if failed(dsGraphBuilder.QueryInterface(IID_IBasicVideo,dsBasicVideo)) then exit; // Get the IVideoWindow Interface if failed(dsGraphBuilder.QueryInterface(IID_IVideoWindow,dsVideoWindow)) then exit; // Get the IMediaEventEx Interface if failed(dsGraphBuilder.QueryInterface(IID_IMediaEventEx,dsMediaEventEx)) then exit; // Attach the IMediaEventEx interface to the main window dsMediaEventEx.SetNotifyWindow(MainForm.Handle,dsEventMessage,dsEventInstance); // Enable IMediaEventEx notifications dsMediaEventEx.SetNotifyFlags(dsEventsOn); Result := True; end; procedure TMainForm.DestroyMovieInterface; var I : Integer; dsFilterEnum : IEnumFilters; dsFilterCount : Integer; dsFetched : uLong; dsFilters : Array[0..49] of IBaseFilter; begin // Stop playback if we're trying to open a movie if one is already loaded. If Assigned(dsMediaControl) then dsMediaControl.Stop; // Remove all filters If Assigned(dsGraphBuilder) then Begin dsFilterEnum := nil; dsFilterCount := 0; I := 0; If dsGraphBuilder.EnumFilters(dsFilterEnum) = S_OK then Begin // Count the currently active filters While (dsFilterEnum.Skip(1) = S_OK) do inc(dsFilterCount); dsFilterEnum.Reset; dsFetched := 0; // Load the filters into an array While (dsFilterEnum.Next(1, dsFilters[I],Addr(dsFetched)) = S_OK) do Inc(I); If Assigned(dsFilterEnum) then dsFilterEnum := NIL; // Remove the filters from the graph If dsFilterCount > 0 then For I := 0 to dsFilterCount-1 do begin dsGraphBuilder.RemoveFilter(dsFilters[I]); dsFilters[I] := nil; end; End; End; // Detach ownership to stop events If Assigned(dsVideoWindow) then Begin dsVideoWindow.put_Visible(false); dsVideoWindow.put_Owner(0); End; // Release all remaining pointers if Assigned(dsMediaControl) then dsMediaControl := NIL; if Assigned(dsMediaSeeking) then dsMediaSeeking := NIL; if Assigned(dsBasicAudio) then dsBasicAudio := NIL; if Assigned(dsBasicVideo) then dsBasicVideo := NIL; if Assigned(dsVideoWindow) then dsVideoWindow := NIL; if Assigned(dsMediaEventEx) then dsMediaEventEx := NIL; if Assigned(dsGraphBuilder) then dsGraphBuilder := NIL; // Clean up the COM interface CoUninitialize; plState := stClosed; end; procedure TMainForm.dsEventPop(var M : TMessage); var dsEventCode : Integer; dsEventParam1 : Integer; dsEventParam2 : Integer; begin inherited; If M.lParam = dsEventInstance then Begin If (Application.Terminated = False) and (Assigned(dsMediaEventEx) = True) then If dsMediaEventEx.GetEvent(dsEventCode,dsEventParam1,dsEventParam2,10) <> E_Abort then