TAudioMediaTypeMost media applications have a configuration option somewhere for the user to select the input audio sampling rate and so on. DirectShow provides the IEnumMediaTypes interface which can be accessed through Delphi with TEnumMediaType.
Our "testbed" project is getting a bit cumbersome now but we'll see if we can add this facility to it. I've resized the form and added a third ListBox - ListBox3
We begin by declaring our TEnumMediaType as a global variable:
var Form1: TForm1; SysDev: TSysDevEnum; AudioDevEnum: TSysDevEnum; PinList: TPinList AudioMediaTypes: TEnumMediaType;
We can create AudioMediaTypes in the ListBox2Click event and assign it items from those items in the PinList which are listed as PINDIR_OUTPUT's. We then simply populate the ListBox with the AudioMediaTypes Items and set a default ListBox3.ItemIndex
: : AudioMediaTypes:= TEnumMediaType.Create; i:= 0; while i < PinList.Count do begin if PinList.PinInfo[i].dir = PINDIR_OUTPUT then begin AudioMediaTypes.Assign(PinList.Items[i]); PinList.Delete(i); end else inc(i); end; for i:= 0 to AudioMediaTypes.Count - 1 do ListBox3.Items.Add(AudioMediaTypes.MediaDescription[i]); ListBox3.ItemIndex:= 0; :
When we run our project now, ListBox3 should list all the Audio formats.
The final part of the puzzle is to configure the Audio Stream with the chosen format. We can do this in the ListBox1Click event before we start to build the Filter graph but we need to configure all of the outputs in the PinList with the media format we've selected from ListBox3 and you'll remember that we deleted the outputs from the PinList.
So, we need to Free the PinList and create a new one. We can then set the format of each output to the format we selected in ListBox3
: : PinList.Free; PinList:= TPinList.Create(Filter2 as IBaseFilter); for i:= 0 to PinList.Count - 1 do begin if PinList.PinInfo[i].dir = PINDIR_OUTPUT then with (PinList.Items[i] as IAMStreamConfig) do SetFormat(AudioMediaTypes.Items[ListBox3.ItemIndex].AMMediaType^); end; FilterGraph1.Mode:= gmCapture; Filter2.FilterGraph:= FilterGraph1; with FilterGraph1 as ICaptureGraphBuilder2 do begin : :
And, finally, don't forget to Free PinList and AudioMediaTypes in the Form's OnCloseQuery event.
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin FilterGraph1.Active:= False; FilterGraph1.ClearGraph; PinList.Free; AudioMediaTypes.Free; end;