Re: TMediaPlayer


[ Related Articles and Replies ] [ DelphiLand Discussion Forum ]

Posted by webmaster Guido on October 02, 2002 at 03:15:59:

In Reply to: TMediaPlayer posted by p12258 Jodell Bumatai on September 26, 2002 at 06:26:04:

: I have followed a tutorial for using the TMediaPlayer component with Delphi 5 STD.
: It plays mp3 files. How can I set a Volume
: Control with TMediaPlayer?
-----

The TMediaPlayer component is only a set of buttons (Play, Stop, Eject,...) that control a multimedia device. You can specify some parameters, like the DeviceType (audio, video,...), Display (the control to show the output: a panel, aform,...), the file to play, and so on...
"Volume" is not one of the properties that you can control from the TMediaPlayer itself, you need some extra programming for that.

The volume of the different channels (wave, midi,...) of an audio device can be controlled with a number of so-called "API-functions" (API = Application Programming Interface). But documentation of these functions can be difficult to find and the interpretation is not so easy, so be warned...

For our purpose, we can use the following two API-functions:

WaveOutGetVolume(ID, @V) gives us the volume V of the audio-device specified by "ID".
WaveOutSetVolume(ID, V) sets the volume to the value V.

For "ID" we can use the predefined constant WAVE_MAPPER.
V is a double word variable (32-bit) that we must provide. The low-order word will contain the volume of the left-channel, and the high-order word is for the right-channel.

Note that not all devices have a volume control. In order to be on the safe side, before using the volume-functions, we can check the capabilities of the waveform-audio output device. This is done with the function WaveOutGetDevCaps(ID, @WaveCaps, SizeOf(WaveCaps)).
ID is the same as for the volume-functions.
WaveCaps is a variable that will receive a "structure" with the capabilities of the audio-device.

How does all this look in code? First, we define the necessary variables. Next, we write a couple of functions to get/set the volume. An example:

var
  WaveCaps: TWAVEOUTCAPS;
  Volume: DWord; 
  VolumeLeft, VolumeRight: Word; // 0xFFFF is full volume, 0x0000 is silence

procedure TForm1.GetVolume;
begin
  // Retrieve the capabilities of waveform-audio output device "WAVE_MAPPER".
  // Structure WaveCaps is filled with info about the capabilities of the device.
  WaveOutGetDevCaps(WAVE_MAPPER, @WaveCaps, SizeOf(WaveCaps));
  // Check if volume control is supported
  if (WaveCaps.dwSupport and WAVECAPS_VOLUME) = WAVECAPS_VOLUME then begin
    // Retrieve the current volume level
    WaveOutGetVolume(WAVE_MAPPER, @Volume);
    VolumeLeft  := LoWord(Volume); 
    VolumeRight := HiWord(Volume); 
  end
  else
    ShowMessage('Volume control not supported');
end;

procedure TForm1.SetVolume;
begin
  WaveOutGetDevCaps(WAVE_MAPPER, @WaveCaps, SizeOf(WaveCaps));
  if (WaveCaps.dwSupport and WAVECAPS_VOLUME) = WAVECAPS_VOLUME then begin
    // Combine left- and right-volume into a double word value
    Volume := VolumeLeft or (VolumeRight shl 16);
    WaveOutSetVolume(WAVE_MAPPER, Volume);
  end
  else
    ShowMessage('Volume control not supported');
end;

How to use these functions? Here's an example for showing the volume in two labels:

  GetVolume;
  labelLeft.Caption  := IntToStr(VolumeLeft);
  labelRight.Caption := IntToStr(VolumeRight);

Setting the volume with the values that were entered in two Edit-boxes is like this:

VolumeLeft  := StrToInt(Edit1.Text); // max is 65535
VolumeRight := StrToInt(Edit2.Text); // max is 65535
Starting on this basis, you can design a nicer interface using slider-controls (TTrackbar component): just one for the volume, left and right treated equally, or two sliders, using the first one for the volume and the other one for the left/right balance. But that's "just math" ;-)

Related Articles and Replies:


[ Related Articles and Replies ] [ DelphiLand Discussion Forum ]