unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, CheckLst;

const
  DLL ='MSndCard.dll';
type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    GroupBox1: TGroupBox;
    CB_Device: TComboBox;
    EditFile: TEdit;
    ButtonDlg: TButton;
    BitBtnOpen: TBitBtn;
    BitBtnPlay: TBitBtn;
    BitBtnPause: TBitBtn;
    BitBtnStop: TBitBtn;
    BitBtnClose: TBitBtn;
    PB_Left: TProgressBar;
    PB_Right: TProgressBar;
    RG_Channel: TRadioGroup;
    Timer1: TTimer;
    Label1: TLabel;
    LabelLen: TLabel;
    Label2: TLabel;
    LabelTime: TLabel;
    GroupBox2: TGroupBox;
    Label3: TLabel;
    LabelLen1: TLabel;
    Label5: TLabel;
    LabelTime1: TLabel;
    EditFile1: TEdit;
    ButtonDlg1: TButton;
    BitBtnOpen1: TBitBtn;
    BitBtnPlay1: TBitBtn;
    BitBtnPause1: TBitBtn;
    BitBtnStop1: TBitBtn;
    BitBtnClose1: TBitBtn;
    PB_Left1: TProgressBar;
    PB_Right1: TProgressBar;
    RG_Channel1: TRadioGroup;
    TrackBarVol: TTrackBar;
    CLB_List: TCheckListBox;
    Label4: TLabel;
    Label6: TLabel;
    LabelStates: TLabel;
    Label7: TLabel;
    LabelStates1: TLabel;
    Label8: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ButtonDlgClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure BitBtnOpenClick(Sender: TObject);
    procedure BitBtnPlayClick(Sender: TObject);
    procedure BitBtnPauseClick(Sender: TObject);
    procedure BitBtnStopClick(Sender: TObject);
    procedure BitBtnCloseClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure RG_ChannelClick(Sender: TObject);
    procedure ButtonDlg1Click(Sender: TObject);
    procedure BitBtnOpen1Click(Sender: TObject);
    procedure BitBtnPlay1Click(Sender: TObject);
    procedure BitBtnPause1Click(Sender: TObject);
    procedure BitBtnStop1Click(Sender: TObject);
    procedure BitBtnClose1Click(Sender: TObject);
    procedure RG_Channel1Click(Sender: TObject);
    procedure TrackBarVolChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  
function MC_GetDeviceList(pDeviceList : Pchar) : integer;  stdcall; external DLL;
function MC_Init( PlayerNum : integer):integer; stdcall; external DLL;
function MC_Free():integer; stdcall; external DLL;
function MC_SetFileName(PlayerID : integer; FileName:Pchar):integer;stdcall;external DLL;
function MC_Open(PlayerID : integer):integer;stdcall;external DLL;
function MC_Play(PlayerID : integer):integer;stdcall;external DLL;
function MC_Pause(PlayerID : integer):integer;stdcall;external DLL;
function MC_Stop(PlayerID : integer):integer;stdcall;external DLL;
function MC_Close(PlayerID : integer):integer;stdcall;external DLL;
function MC_GetStates(PlayerID : integer ):integer;stdcall;external DLL;
function MC_GetFileDuration(PlayerID : integer ):Dword;stdcall;external DLL;
function MC_GetPosition(PlayerID : integer) : Dword;stdcall;external DLL;
function MC_SetPosition(PlayerID : integer; dwPos : Dword) : integer;stdcall;external DLL;
function MC_SetVolume(PlayerID : integer; Volume : Byte) : integer;stdcall;external DLL;
function MC_GetVolume(PlayerID : integer) : integer;stdcall;external DLL;
function MC_SelectDevice(PlayerID : integer; DeviceID:Byte) : integer;stdcall;external DLL;
function MC_SetPan(PlayerID : integer; PanID:Byte) : integer;stdcall;external DLL;
function MC_GetVU(PlayerID:integer;ChannelID:Byte) : Word; stdcall;external DLL;
function MC_SelectDeviceList(PlayerID : integer; DeviceListID:Word) : integer;stdcall;external DLL;
function MC_RegistryFilter(CodecsPath:Pchar) : integer;stdcall;external DLL;
function MC_SetSN(SN:Pchar) : integer;stdcall;external DLL;

implementation

{$R *.dfm}
const
  ID_NO1 = 0;
  ID_NO2 = 1;

function MTimeToTimeStr( mTime : dWord ) : String;
var
  HH,MM,SS : Word;
begin
  HH:=mTime div 3600 ;
  MM:=mTime - 3600 * HH;
  SS:=MM mod 60;
  MM:=MM div 60;
  Result:=Format('%.2d:%.2d:%.2d',[HH,MM,SS]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  cnt : integer;
  pList : array [0..1024] of char;
begin
  MC_RegistryFilter(Pchar(ExtractFilePath(ParamStr(0))+'Codecs'));

  MC_SetSN('请输入注册码!');

  MC_GetDeviceList(pList);
  CB_Device.Items.Text:=Strpas(pList);
  if CB_Device.Items.Count > 0 then CB_Device.ItemIndex:=0;
  CLB_List.Items.Text:=CB_Device.Items.Text;

  for cnt:=0 to CLB_List.Items.Count-1 do
    begin
     CLB_List.Checked[cnt]:=True;
    end;

  MC_Init(2);
end;

procedure TForm1.ButtonDlgClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    begin
      EditFile.Text:=OpenDialog1.FileName;
    end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  MC_Free();
end;

procedure TForm1.BitBtnOpenClick(Sender: TObject);
begin
 MC_SelectDevice(ID_NO1,CB_Device.ItemIndex);
 MC_SetFileName(ID_NO1,Pchar(EditFile.Text));
 if MC_Open(ID_NO1)=0 then BitBtnPlay.Click;
end;

procedure TForm1.BitBtnPlayClick(Sender: TObject);
begin
  MC_Play(ID_NO1);
end;

procedure TForm1.BitBtnPauseClick(Sender: TObject);
begin
  MC_Pause(ID_NO1);
end;

procedure TForm1.BitBtnStopClick(Sender: TObject);
begin
  MC_Stop(ID_NO1);
end;

procedure TForm1.BitBtnCloseClick(Sender: TObject);
begin
  MC_Close(ID_NO1);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  PB_Left.Position:=MC_GetVU(ID_NO1,0);
  PB_Right.Position:=MC_GetVU(ID_NO1,1);

  LabelLen.Caption:=MTimeToTimeStr(MC_GetFileDuration(ID_NO1) div 1000);
  LabelTime.Caption:=MTimeToTimeStr(MC_GetPosition(ID_NO1) div 1000);

  // (sUnOpend,sOpened,sPlaying,sPaused,sStopped,sEnded);
//  LabelStates.Caption:=Inttostr(MC_GetStates(ID_NO1));
  case MC_GetStates(ID_NO1) of
    0 : LabelStates.Caption:='未打开';
    1 : LabelStates.Caption:='打开';
    2 : LabelStates.Caption:='播放';
    3 : LabelStates.Caption:='暂停';
    4 : LabelStates.Caption:='停止';
    5 : LabelStates.Caption:='结束';
  end;

  PB_Left1.Position:=MC_GetVU(ID_NO2,0);
  PB_Right1.Position:=MC_GetVU(ID_NO2,1);

  LabelLen1.Caption:=MTimeToTimeStr(MC_GetFileDuration(ID_NO2) div 1000);
  LabelTime1.Caption:=MTimeToTimeStr(MC_GetPosition(ID_NO2) div 1000);

//  LabelStates1.Caption:=Inttostr(MC_GetStates(ID_NO2));
  case MC_GetStates(ID_NO2) of
    0 : LabelStates1.Caption:='未打开';
    1 : LabelStates1.Caption:='打开';
    2 : LabelStates1.Caption:='播放';
    3 : LabelStates1.Caption:='暂停';
    4 : LabelStates1.Caption:='停止';
    5 : LabelStates1.Caption:='结束';
  end;

end;

procedure TForm1.RG_ChannelClick(Sender: TObject);
begin
  MC_SetPan(ID_NO1,RG_Channel.ItemIndex);
end;

procedure TForm1.TrackBarVolChange(Sender: TObject);
begin
  MC_SetVolume(ID_NO1,TrackBarVol.Position);
end;

procedure TForm1.ButtonDlg1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    begin
      EditFile1.Text:=OpenDialog1.FileName;
    end;
end;

procedure TForm1.BitBtnOpen1Click(Sender: TObject);
var
  CardList,cnt : integer;
begin
 CardList:=0;

 for cnt:=0 to CLB_List.Count-1 do
  if CLB_List.Checked[cnt] then
   begin
     CardList:=CardList + ( 1 shl cnt);
   end;

 if CardList = 0 then
   begin
     Showmessage('请选择声卡设备!');
     Exit;
   end;
   
 MC_SelectDeviceList(ID_NO2,CardList);

 MC_SetFileName(ID_NO2,Pchar(EditFile1.Text));

 if MC_Open(ID_NO2)=0 then BitBtnPlay1.Click;
 
end;

procedure TForm1.BitBtnPlay1Click(Sender: TObject);
begin
   MC_Play(ID_NO2);
end;

procedure TForm1.BitBtnPause1Click(Sender: TObject);
begin
   MC_Pause(ID_NO2);
end;

procedure TForm1.BitBtnStop1Click(Sender: TObject);
begin
   MC_Stop(ID_NO2);
end;

procedure TForm1.BitBtnClose1Click(Sender: TObject);
begin
   MC_Close(ID_NO2);
end;

procedure TForm1.RG_Channel1Click(Sender: TObject);
begin
 MC_SetPan(ID_NO2,RG_Channel1.ItemIndex);
end;

end.
 