WEB开发网
开发学院软件开发VC 如何播放大型WAV文件 阅读

如何播放大型WAV文件

 2008-02-26 20:27:22 来源:WEB开发网   
核心提示:播放完所有的WAV文件后必须关闭波形音频设备,Cwave类的析构函数调用Cwave::Close Device自动完成,如何播放大型WAV文件(2), 将本文中介绍的CWave类加入到自己的程序中,就可以方便的应用它播放音频文件了,//建立Cwave类,放在Wave.h文件中:class CWave:public CO

播放完所有的WAV文件后必须关闭波形音频设备,Cwave类的析构函数调用Cwave::Close Device自动完成。 将本文中介绍的CWave类加入到自己的程序中,就可以方便的应用它播放音频文件了。

//建立Cwave类,放在Wave.h文件中:

class CWave:public CObject
{
  //Construction
  public:
    CWave();
    virtual ~CWave();
  //Operations
public:
    DWORD OpenDevice();
    DWORD CloseDevice();
    DWORD Play(CWnd *pParentWnd,LPCSTR pFileName);
    DWORD Stop();
  //Implementation
  protected:
    void DisplayErrorMsg(DWORD dwError);
  //Members
  protected:
    MCIDEVICEID m_nDeviceID;
    MCIDEVICEID m_nElementID;
};
//Cwave类的实现代码,Cwave.cpp
#include <stdafx.h>
#include "cwave.h"
CWave::CWave()
{
  m_nDeviceID=0;
  m_nElementID=0;
}
CWave::~CWave()
{
  if(m_nElementID)
    Stop();
  if(m_nDeviceID)
    CloseDevice();
}
DWORD CWave::OpenDevice()
{
  DWORD dwResult=0;
  if (m_nDeviceID)
  {
    MCI_OPEN_PARMS mciOpenParms;
    mciOpenParms.lpstrDeviceType=(LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
    
    //open the wave device
    dwResult = mciSendCommand(NULL,
        MCI_OPEN,
        MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,
        (DWORD)(LPVOID)&mciOpenParms);
    //save device identifier,will use eith other MCI commands
    m_nDeviceID = mciOpenParms.wDeviceID;
    //display error message if failed
    if(dwResult)
       DisplayErrorMsg(dwResult);
   }
   //return result of MCI operation
   return dwResult;
}
DWORD CWave::CloseDevice()
{
  DWORD dwResult=0;
  //close if currently open
  if(m_nDeviceID)
  {
    //close the MCI device
    dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL);
    //display error message if failed
    if(dwResult)
      DisplayErrorMsg(dwResult);
    //set identifier to close state
    else
      m_nDeviceID=0;
  }
  //return result of MCI operation
  return dwResult;
}
DWORD CWave::Play(CWnd* pWnd,LPCSTR pFileName)
{
  MCI_OPEN_PARMS mciOpenParms;
  //initialize structure
  memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));
  //set the WAV file name to be played
  mciOpenParms.lpstrElementName=pFileName;
  //first open the device
  DWORD dwResult=mciSendCommand(m_nDeviceID,MCI_OPEN,
    MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
  //display error message if failed
  if(dwResult)
    DisplayErrorMsg(dwResult);
  //if successful,instruct the device to play the WAV file
  else
  {
    //save element indentifier
    m_nElementID=mciOpenParms.wDeviceID;
    MCI_PLAY_PARMS mciPlayParms;
    //set the window that will receive notification message
    mciPlayParms.dwCallback=(DWORD)pWnd->m_hWnd;
    //instruct device to play file
    dwResult=mciSendCommand(m_nElementID,MCI_PLAY,
      MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
    //display error and close element if failed
    if(dwResult)
    {
      DisplayErrorMsg(dwResult);
      Stop();
    }
  }
  //return result of MCI operation
  return dwResult;
}
DWORD CWave::Stop()
{
  DWORD dwResult=0;
  //close if element is currently open
  if(m_nElementID)
  {
    dwResult=mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL);
    //display error message if failed
    if(dwResult)
      DisplayErrorMsg(dwResult);
    //set identifier to closed state
    else
      m_nElementID=0;
  }
  return dwResult;
}
void CWave::DisplayErrorMsg(DWORD dwError)
{
  //check if there was an error
  if(dwError)
  {
    //character string that contains error message
    char szErrorMsg[MAXERRORLENGTH];
    //retrieve string associated error message
    if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg)))
      strcpy(szErrorMsg,"Unknown Error");
    //display error string in message box
    AfxMessageBox(szErrorMsg);
  }
}

上一页  1 2 

Tags:如何 播放 大型

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接