Nowadays, I have a multimedia play tool, which is a very happy and pleasant thing, but if you use MediaPlay control, it will be a lot of restrictions, and you have a lot of good creative ideas that you can't achieve, if you use Microsoft DirectX. Interface development can fully put the author's unique idea into realization, why not! ! However, the development instructions of the DirectShow interface is really small, only some of them are some of the theoretical, truly on interface actual programming and use Delphi development tools to achieve a phoenix, making many people It is still a step. Here, I applied the experience of DirectShow development and I collected some information to reiniterate editing, I hope that all the interested colleagues will help, even if I reach my purpose. Less nonsense, enter the body.
Since it is an interface, it will first list some common interfaces, so that everyone has some basic understanding, what we need to do, when we need to use this interface.
IFiltergraph filter channel interface ifiltergraph2 enhanced iFiltergraphiGraphbuilder
The most reused COM interface, used to manually or automatically construct filtration channel Filter Graph Manager
IMEDIACONTROL is used to control streaming media, such as streaming and stop pause, etc., play the control interface IMEDIAEVENT play event interface, which uses some events in Filter Graph to create the flag information and transfer to the application IMEDIAEVENTEX extension play event interface IMEDIAPSITION Playback location and speed control interface (control play position can only be set to set time control) iMediaseEKing
Another playback location and playback speed control interface, in the location selection. Set the play format, multiple control playback mode. Commonly available: (1) Time_Format_Media_time unit 100 nanosecond. (2) Time_Format_Frame Press frame playback
IBASICAUDIO Sound Control Interface IBASICVIDEO Image Control Interface (Porter Rate, Width, Length, etc. Information) iVideoWindow Display Window Control Interface (All Controls for Playback Windows, including CAPTION Display, Window Position Control, etc.) Isamplegrabber capture image interface (available for catching Graph Control) IvideoFrameStep Control Single Frame Play Interface
Ok, I am familiar with the application of DirectShow application development, we are familiar with these interfaces through an instance media player. Although the code of the instance is simple, but the five organs are full, the function is powerful, and it also understands the application of DirectShow development. A step of.
Generally, generally use the DirectShow interface to program the programming, initialize the interface, using the control function in the interface, and finally release the interface. (Of course, this is assumed here that you already have a unit you must have units, if you don't have, please ask for it online (Note: The following variables are not defined, you need to define your own use)
1) Initializing the interface part
First, you need to define the interface variables you need to use.
Graphbuilder: IGRAPHBUILDER;
MediaControl: iMediacontrol;
Mediaseeking: iMediaseEKing;
MediaPosition: IMEDIAPSITION;
MediaeventEx: iMediaEvent;
Basicaudio: ibasicaudio;
BasicVideo: ibasicvideo;
VideoWindow: iVideoWindow;
Samplegrabber: isamplegrabber;
VideoFramestep; (1) Then you need to create a Filter Graph Manager instance using the CocreateInstance function, CocreateInstance (Tguid (CLSID_FILTERGRAPH), NIL, CLSCTX_INPROC_SERVER,
Tguid (IID_IGRAPHBUILDER), Graphbuilder
Because you need to use the IsAmpleGrabber interface, you need to create a SampleGRabber instance.
Var Filter: ibasefilter;
CocreateInstance (CLSID_SAMPLEGRABBER, NIL, CLSCTX_INPROC_SERVER,
IID_IBASEFILTER, FILTER);
(2) Call QueryInterface function acquisition to get pointers, after the operation is controlled
Filter.QueryInterface (IID_ID_ISAMPLEGRABBER, SAMPLEGRABBER);
Graphbuilder.addfilter (Filter, '' grabber ');
Graphbuilder.QueryInterface (IID_IMEDIACONTROL, MediaControl);
Graphbuilder.QueryInterface (IID_IMEDIAPSITION, MedIaPosition);
Graphbuilder.QueryInterface (iid_imediaseeking, mediaseeping);
Graphbuilder.queryinterface (IID_IMEDIAEVENTEX, MediaEventex);
Graphbuilder.queryinterface (IID_IVIDEOFRAMESTEP, VideoFramestep);
Graphbuilder.Queryinterface (IID_IBASICAUDIO, BASICAUDIO);
Graphbuilder.Queryinterface (IID_IBASICVIDEO, BASICVIDEO);
Graphbuilder.queryinterface (IID_IVideoWindow, VideoWindow);
Of course, for the sake of safety, it can be successfully judged, give information, otherwise it is very likely that there is a problem that the clue cannot be found. Ok, everything is ready to be successful, you can enter the third step and start our control operation.
(3) Start control through the function provided by the interface
Oh, I almost forget an important thing, and two important things to do before calling QueryInterface above, first, to create a Unicode (Wide Character) string, save the file name.
Var _wfile: array [0..max_path - 1] of wchar;
MultibyToWideChar (CP_ACP, 0, PCHAR (play file name), -1, @_wfile, max_path;
Then you need to success Renderfile to control the operation graphbuilder.renderfile (_wfile, nil);
Of course, when you are displayed, you need to connect the display form and control, here you need to pass the iVideoWindow interface method, VideoWindow. Put_owner (edit1.handle);
VideoWindow. Put_windowstyle (dsvideo_window_child_style);
VideoDow.SetWindowPosition (0, 20, Edit1.ClientWidth, Edit1.ClientHeight);
Get some of the necessary information of the image, use the method in the iBasicVideo interface, some variables are defined by themselves, BasicVideo.GetVideSize (VideoWidth, VideoHEOHT);
BasicVideo.get_bitrate (videobitch);
BasicVideo.get_avgtimeperframe (perframe);
Get the total time and play time of the current file, you need to use the iMediaseEKing interface method,
MediaseEKing.getduration (DURATION); // Get total time
MediaseEKing.getCurrentPosition (currentpos); // Get the current play time
You can also set the start and end time via the iMediaseEKing :: setPositions method.
Oh, the unit you get here seems to be millimeters, you can convert yourself into seconds.
Also, if you want a single frame to control playback, you also need to set the play format to play with frame.
MediaseEKing.SetTimeFormat (Time_Format_frame);
Play, stop, pause, etc.
These needs to use the iMediaControl interface, which is simple, respectively,
MediaControl.play;
MediaControl.stop;
MediaControl.Pause
Playing speed setting
A method of using iMediaPosition is required.
MediaPosition.put_rate (1); // Normal
MediaPosition.put_rate (0.25); // Slow
MediaPosition.put_rate (2); // Fast
Single frame playback control
Need to use iVideoFramestep
VideoFramestep.Step (1, nil);
volume control
Need to use ibasicaudio
Increase volume: Basicaudio.get_volume (& volume); // Get volume Volume: = Volume VolumeStep; Basicaudio.Put_Volume (Volume); // Add a decibel minus volume: Basicaudio.get_volume (& volume); // Get Volume: = Volume -VolumeStep; Basicaudio.putVolume (Volume); // Decrease a certain amount of decibels
Display zoomal reduction control
Simply change the size of Edit1, then use the iVideoWindow interface method
VideoDow.SetWindowPosition (0, 0, Edit1.Width, Edit1.Height);
Single frame capture, grasp
In fact, many interfaces provide this feature, but I prefer to use the isamplegrabber interface to implement, relatively, efficient.
This control is a little more, first, when opening the file
Var MediaType: TAM_MEDIA_TYPE;
ZeromeMory (@MediaType, sizeof (tam_media_type));
MediaType.majortype: = mediatype_video; // video stream
MediaType.subtype: = MediaSubType_RGB24; // 24 bit image
MediaType.formattype: = format_videoInfo;
Samplegrabber.setMediatype (MediaType); // Association interface
Samplegrabber.setBuffersamples (TRUE);
Then you click on the graphic button event.
MediaType: TAM_MEDIA_TYPE;
VideoInfoHeader: TVideoInfoHead;
BitmapInfo: TbitmapInfo;
Bitmap: hbitmap;
Buffer: Pointer;
Buffersize: integer;
Begin
Samplegrabber.getConnectedMediatype (MediaType);
ZeromeMory (@VideoInfoHeader);
CopyMemory (@VideoInfoHeader, MediaType.pbformat, sizeof (VideoInfoHeader);
ZeromeMory (@bitmapinfo, sizeof (tbitmapinfo);
CopyMemory (@BitmapInfo, @ VideoInfoHeader.bmiheader, sizeof (VideoInfoHeader.bmihead);
Bitmap: = Createdibsection (0, BitmapInfo, DIB_RGB_COLORS, BUFFER, 0, 0);
Samplegrabber.getcurrentBuffer (Buffersize, Buffer);
Image1.picture.bitmap.handle: = Bitmap
END;
I.e.