2

I'm trying to add functionality to a program that gives the option to select a sound card out of a combobox and then using that sound card for output on a TMediaPlayer. Here's the code I have so far but the MediaPlayer keeps on playing on the default sound card.

unit Select_SoundCard_Update;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
   Forms,
    Dialogs, MMSystem, StdCtrls, MPlayer;

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    ComboBox1: TComboBox;
    btn1: TButton;
    dlgOpen1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1CloseUp(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
   { Private declarations }
  public
   { Public declarations }
 end;

var
   Form1: TForm1;
   outcaps: TWaveOutCaps;
   parms: MCI_WAVE_SET_PARMS;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
 ShowMessage('Jy het '+IntToStr(waveOutGetNumDevs) + ' soundcard(s) wat 
 opgetel word.');
i := waveOutGetNumDevs;
for i := 0 to i - 1 do
   begin
      waveOutGetDevCaps(i, @outcaps, SizeOf(outcaps));
      Combobox1.Items.Add(outcaps.szPname);
   end;
   Combobox1.ItemIndex := 0;
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
   MediaPlayer1.Close;
   parms.wOutput := Combobox1.itemindex;
   mciSendCommand(MediaPlayer1.DeviceID, MCI_SET, MCI_WAVE_OUTPUT, 
   Longint(@parms));
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
   ShowMessage(IntToStr(MediaPlayer1.DeviceID));
  MediaPlayer1.Close;
if dlgOpen1.Execute then
   begin
     MediaPlayer1.FileName := dlgOpen1.FileName;
     MediaPlayer1.Open;
   end;
end;

end.

What am I doing wrong? Please any advice would be appreciated have been at this for days now. Thanks in advance.

  • How many sound cards do you have? – Freddie Bell Nov 13 '17 at 12:17
  • Do you have any error messages? You need to investigate the results of initialization of another sound card, as also to see which is the result of playing on it. That will give you the reason for failing on accomplish that, – RBA Nov 13 '17 at 12:21
  • @nolaspeaker I have 2 sound cards installed. – Adriaan Roets Nov 13 '17 at 15:36
  • @RBA I don't get any error messages with that code. The program just simply continues to play on the default sound card. – Adriaan Roets Nov 13 '17 at 15:36
  • Not sure about that (I've always used waveOutXxx commands) but isn't `TMediaPlayer` an encapsulation for all `MCI_xxx` stuff? You use `mciSendCommand`method, but couldn't you set output device ID using `MediaPlayer1` variable? – paradise Nov 13 '17 at 16:26
  • 1
    `TMediaPlayer.Close()` is being called in `ComboBox1CloseUp()`, so the `DeviceID` is no longer valid when `MCI_WAVE_OUTPUT` is issued. You need to send the command while `TMediaPlayer` is open – Remy Lebeau Nov 13 '17 at 16:29
  • @RemyLebeau Thanks, however I first tried it without closing the TMediaPlayer, also didn't work. Is it not that windows doesn't support changing the soundcard while the mediaplayer is active? I tried destroying it as well but got an access violation error. – Adriaan Roets Nov 13 '17 at 17:07
  • @paradise Using 'MediaPlayer1' variable? I'm not exactly sure what you mean? – Adriaan Roets Nov 13 '17 at 17:10
  • @AdriaanRoets: if I had to guess, `mciSendCommand()` is failing with an error that you are not checking for. What is the actual return value? – Remy Lebeau Nov 13 '17 at 17:57
  • @RemyLebeau I have no idea what it's returning at the moment, any way I could test it? – Adriaan Roets Nov 13 '17 at 20:46
  • @AdriaanRoets `mciSendCommand()` returns a `MCIERROR` (alias for `DWORD`), what value is being returned? Anything other than 0 is an error – Remy Lebeau Nov 13 '17 at 21:03
  • mciSendCommand() return 257 value that mean MCIERR_INVALID_DEVICE_ID, so check it. https://msdn.microsoft.com/en-us/library/aa228215(v=vs.60).aspx – Aqil Nov 13 '17 at 22:19
  • @Aqil So the deviceID that I'm setting is invalid? Or is it the 'MediaPlayer.DeviceID' that gives the invalid DeviceID error? – Adriaan Roets Nov 14 '17 at 05:42
  • @AdriaanRoets none of them, the mciSendCommand() parameter are notvalid. i do not have 2 sound cart, but try to set deviceId using MediaPlayer1.DeviceID. get your two devices id by outcaps.wMid. and let me know what happend. – Aqil Nov 14 '17 at 20:56
  • `MCIERR_INVALID_DEVICE_ID` --> "Invalid device ID. Use the ID given to the device when the device was opened." https://msdn.microsoft.com/en-us/library/windows/desktop/dd797980(v=vs.85).aspx – paradise Nov 14 '17 at 21:27
  • Beware that `parms.wOutput` valid values starts from 1, as `Combobox1.itemindex` could be 0. What happens if you try to set the value manually? `parms.wOutput := 2` for example? – paradise Nov 14 '17 at 21:30
  • @Aqil Can you please get a precise mciSendCommand() command? Where would I use outcaps.wMid in the code? – Adriaan Roets Nov 15 '17 at 18:44
  • @paradise Thanks that's good to know. How would I then use the device ID to set parms.wOutput correctly to the correct device? – Adriaan Roets Nov 15 '17 at 18:46
  • I was wrong, `DeviceId` is `readonly` so it is not possible to assign it directly, i suggest you check `Vcl.MPlayer` Unit. it has a `open` `procedure` that will help you. – Aqil Nov 16 '17 at 14:06

0 Answers0