+ -
当前位置:首页 → 问答吧 → MCI_PLAY_PARMS dwCallback 如何获取回调事件

MCI_PLAY_PARMS dwCallback 如何获取回调事件

时间:2011-12-09

来源:互联网

我的问题就是使用mciSendCommand如何获取当前播放音乐时间点,精度是毫秒。
回调事件如何捕捉,请高手指点,困惑了很久了。谢谢!

代码如下

unit mciPas;

interface

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

type
  TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  mciOpenParms: MCI_OPEN_PARMS;
  m_MCIDeviceID: MCIDEVICEID;
  mciPlayParms:MCI_PLAY_PARMS;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//初始化参数
procedure TForm1.Button1Click(Sender: TObject);
VAR
  file_name_: String;
begin
  file_name_ := 'D:\test.wma';
  mciOpenParms.lpstrDeviceType:='''';
  mciOpenParms.lpstrElementName := PChar(file_name_);
  mciSendCommand(0,MCI_OPEN,MCI_OPEN_ELEMENT,DWORD(@mciOpenParms));
  m_MCIDeviceID := mciOpenParms.wDeviceID;
  mciPlayParms.dwCallback := Handle;
  mciPlayParms.dwFrom := 29*1000;
  mciPlayParms.dwTo := mciPlayParms.dwFrom+1500;
end;

//开始播放
procedure TForm1.Button2Click(Sender: TObject);
begin
 mciSendCommand(m_MCIDeviceID,MCI_PLAY,MCI_FROM or MCI_TO or MCI_NOTIFY,integer(@mciPlayParms));
end;

//如何获取到当前播放时间点
/*
windows sdk 说明

The MCI_PLAY_PARMS structure contains positioning information for the MCI_PLAY command. 

typedef struct {
  DWORD dwCallback;  
  DWORD dwFrom; 
  DWORD dwTo; 
} MCI_PLAY_PARMS;
 

Members

dwCallback

The low-order word specifies a window handle used for the MCI_NOTIFY flag.

dwFrom

Position to play from.

dwTo

Position to play to. 

Remarks

When assigning data to the members of this structure, set the corresponding flags in the fdwCommand parameter of the mciSendCommand function to validate the members.  
*/

end.

作者: yhcustc   发布时间: 2011-12-09

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  Button3: TButton;
  Button4: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure Button4Click(Sender: TObject);
  private
  { Private declarations }
  function GetPosition():DWORD;
  procedure MCINotify(Var Msg:TMessage);Message MM_MCINOTIFY;
  public
  { Public declarations }
  mciOpenParms: MCI_OPEN_PARMS;
  m_MCIDeviceID: MCIDEVICEID;
  mciPlayParms:MCI_PLAY_PARMS;


  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetPosition():DWORD; // 获取播放位置(毫秒)
var
  mci_Pos : MCI_STATUS_PARMS;
  ret: integer;
  pszText: PChar; uLength: UINT;
begin
  Result := 0;
  mci_Pos.dwCallback := Handle;
  mci_Pos.dwItem := MCI_STATUS_POSITION;
  ret:=mciSendCommand(
  m_MCIDeviceID,
  MCI_STATUS,
  MCI_WAIT or MCI_STATUS_ITEM,
  DWORD(@mci_Pos));
  if 0<>ret then
  begin
  uLength := 255;
  GetMem( pszText, uLength );
  mciGetErrorString(ret, pszText, uLength);
  self.Caption := pszText;
  FreeAndNil( pszText );
  end;
  Result := mci_Pos.dwReturn;
end;

procedure TForm1.MCINotify(Var Msg:TMessage);
begin
  case (Msg.wParam) of
  MCI_NOTIFY_FAILURE:
  showmessage('出现错误,播放停止!');
  MCI_NOTIFY_ABORTED: showmessage('ABORTED!');
  MCI_NOTIFY_SUPERSEDED:showmessage('SUPERSEDED!');
  MCI_NOTIFY_SUCCESSFUL://showmessage();
  begin
  self.Caption := '播放结束!';
  end;
  else
  showmessage('UNKOWN!');
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR
  file_name_: String;
begin
  file_name_ := 'D:\test.wma';
  mciOpenParms.lpstrDeviceType:='';
  mciOpenParms.lpstrElementName := PChar(file_name_);
  mciSendCommand(0,MCI_OPEN,MCI_OPEN_ELEMENT,DWORD(@mciOpenParms));
  m_MCIDeviceID := mciOpenParms.wDeviceID;
  mciPlayParms.dwCallback := Handle;
  mciPlayParms.dwFrom := 1;
  mciPlayParms.dwTo := mciPlayParms.dwFrom+2000*10;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  mciSendCommand(m_MCIDeviceID,MCI_PLAY,MCI_FROM or MCI_TO or MCI_NOTIFY,integer(@mciPlayParms));
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  self.Caption := inttostr(GetPosition());
end;

end.

作者: extcsdn   发布时间: 2011-12-13

热门下载

更多