录音机(04_MzRecord)

录音机

此示例代码展示如何使用录音组件。

注 : 按下暂停键后,再次按下开始录音即可继续录音,不会重新开始

04_MzRecord.png
 获取录音时间:
    

    WCHAR time[100];
    int gettime = m_pRecorder->GetTime()/1000;
    wsprintf(time, L"已录制%d秒", gettime);
    m_Time.SetText(time);
    m_Time.Invalidate();
 三个底层设备消息响应函数:
    

    virtual LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            case MM_MIXM_CONTROL_CHANGE:
            case MM_WIM_DATA:
            {
                if(m_pRecorder)
                {
                    m_pRecorder->OnMM_WIM_DATA(message, wParam, lParam);
                }
            }
            break;
            case MM_WIM_OPEN:
            {
                if(m_pRecorder)
                {
                    m_pRecorder->OnMM_WIM_OPEN(MM_WIM_OPEN, wParam, lParam);
                }
            }
            break;

            case MM_WIM_CLOSE:
            {
                if(m_pRecorder)
                {
                m_pRecorder->OnMM_WIM_CLOSE(MM_WIM_CLOSE, wParam, lParam);
                }
            }
            break;
        }
        return CMzWndEx::MzDefWndProc( message, wParam, lParam );
    }

完整示例

/************************************************************************/
/*
* Copyright (C) Meizu Technology Corporation Zhuhai China
* All rights reserved.
* 中国珠海, 魅族科技有限公司, 版权所有.
*
* Author:    ZYK
* Create on: 2009-07-14
*/
/************************************************************************/

//请按照以步骤运行此实例代码:
//首先, 打开VS2005/2008创建一个Win 32智能设备项目
//在项目向导中选择M8SDK, 并勾选空项目
//然后,在项目中新建一个cpp文件,将此处代码拷贝到cpp文件中
//最后,按照M8SDK的帮助文档,配置项目属性
//现在,可以运行此程序了

//包含MZFC库的头文件
#include <mzfc_inc.h>
#include <initguid.h>
#include <IRecorder.h>
#include <IRecorder_Guid.h>
#include <IMixer.h>
//此代码演示了:
//  创建和初始化应用程序
//  创建和初始化窗体
//  IRecorder组件的使用

// 按钮控件的ID
#define MZ_IDC_STARTBTN  101
#define MZ_IDC_STOPBTN   102
#define MZ_IDC_PAUSEBTN  103

#define TEST_BTN_WIDTH  120
#define TEST_BTN_HEIGHT  100

#define TIMER_ID         10
// 从 CMzWndEx 派生的主窗口类
class CSample1MainWnd: public CMzWndEx
{
  MZ_DECLARE_DYNAMIC(CSample1MainWnd);
public:
  // 窗口中的按钮控件
  UiButton m_StartBtn;
  UiButton m_StopBtn;
  UiButton m_PauseBtn;

  UiStatic m_Time;
  UiStatic m_Tip;

  IRecord *m_pRecorder;
protected:
  void StartRecord()
  {
    if (!m_pRecorder)
    {
      return ;
    }

  SetTimer(m_hWnd, TIMER_ID, 1000, NULL);
  //开始录音
  m_pRecorder->RecStart();
  }
  
  void StopRecord()
  {
    //停止录音
    m_pRecorder->RecStop();

    if (m_pRecorder)
    {
      m_pRecorder->Release();
    }
  }

  void PauseRecord()
  {
    //暂停录音
    m_pRecorder->RecPause();
  }

  // 窗口的初始化
  virtual BOOL OnInitDialog()
  {
    // 必须先调用基类的初始化
    if (!CMzWndEx::OnInitDialog())
    {
      return FALSE;
    }

    if(FAILED( CoCreateInstance(CLSID_Recorder,NULL,CLSCTX_INPROC_SERVER,IID_MZ_Record,(void **)&m_pRecorder)))
    {
      return FALSE;
    }

    m_pRecorder->SetCallBackWindow(m_hWnd, RECORDER_MODE);

    m_pRecorder->CreateFileName();

    int width = GetWidth();
    int height = GetHeight();
    int btnSpace = (width - 3 * TEST_BTN_WIDTH) / 4;
    int left = btnSpace;
    int top = height - btnSpace - TEST_BTN_HEIGHT;

     m_StartBtn.SetButtonType(MZC_BUTTON_GREEN);
     m_StartBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT  );
     m_StartBtn.SetID(MZ_IDC_STARTBTN);
     m_StartBtn.SetText(L"开始");
     m_StartBtn.SetTextColor(RGB(255,255,255));
     AddUiWin(&m_StartBtn);

     left += TEST_BTN_WIDTH + btnSpace;

     m_PauseBtn.SetButtonType(MZC_BUTTON_GREEN);
     m_PauseBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT);
     m_PauseBtn.SetID(MZ_IDC_PAUSEBTN);
     m_PauseBtn.SetText(L"暂停");
     m_PauseBtn.SetTextColor(RGB(255,255,255));

     left += TEST_BTN_WIDTH + btnSpace;

     AddUiWin(&m_PauseBtn);
 
     m_StopBtn.SetButtonType(MZC_BUTTON_GREEN);
     m_StopBtn.SetPos(left, top, TEST_BTN_WIDTH, TEST_BTN_HEIGHT);
     m_StopBtn.SetID(MZ_IDC_STOPBTN);
     m_StopBtn.SetText(L"停止");
     m_StopBtn.SetTextColor(RGB(255,255,255));
 
     AddUiWin(&m_StopBtn);

     m_Time.SetPos(100, 250, 280, 100);
     m_Time.SetText(L"已录制0秒");
     AddUiWin(&m_Time);

     m_Tip.SetPos(0, 400, 480, 200);
     m_Tip.SetText(L"提示:生成的文件保存于:\\Disk\\Recorder下");
     AddUiWin(&m_Tip);

    return TRUE;
  }

  // 重载命令消息的处理函数
  virtual void OnMzCommand(WPARAM wParam, LPARAM lParam)
  {
    UINT_PTR id = LOWORD(wParam);
    switch(id)
    {
    case MZ_IDC_STARTBTN:
      {
        StartRecord();
      }
      break;
    case MZ_IDC_PAUSEBTN:
      {
        PauseRecord();
      }
      break;
    case MZ_IDC_STOPBTN:
      {
        StopRecord();
      }
      break;
    }

  }
  
  virtual void OnTimer(UINT_PTR nIDEvent)
  {
    switch(nIDEvent)
    {
    case TIMER_ID:
      {
        WCHAR time[100];
        int gettime = m_pRecorder->GetTime()/1000;
        wsprintf(time, L"已录制%d秒", gettime);
        m_Time.SetText(time);
        m_Time.Invalidate();
      }
      break;
    }
  }
  virtual LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
  {
    switch(message)
    {
    case MM_MIXM_CONTROL_CHANGE:
    case MM_WIM_DATA:
      {
        if(m_pRecorder)
        {
          m_pRecorder->OnMM_WIM_DATA(message, wParam, lParam);
        }
      }
    break;
    case MM_WIM_OPEN:
      {
        if(m_pRecorder)
        {
          m_pRecorder->OnMM_WIM_OPEN(MM_WIM_OPEN, wParam, lParam);
        }
      }
    break;
          
    case MM_WIM_CLOSE:
      {
        if(m_pRecorder)
        {
          m_pRecorder->OnMM_WIM_CLOSE(MM_WIM_CLOSE, wParam, lParam);
        }
      }
     break;
    }
    return CMzWndEx::MzDefWndProc( message, wParam, lParam );
  }
};

MZ_IMPLEMENT_DYNAMIC(CSample1MainWnd)

// 从 CMzApp 派生的应用程序类
class CSample1App: public CMzApp
{
public:
  // 应用程序的主窗口
  CSample1MainWnd m_MainWnd;

  // 应用程序的初始化
  virtual BOOL Init()
  {
    // 初始化 COM 组件
    CoInitializeEx(0, COINIT_MULTITHREADED);

    // 创建主窗口
    RECT rcWork = MzGetWorkArea();
    m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0);
    m_MainWnd.Show();

    // 成功则返回TRUE
    return TRUE;
  }
};

// 全局的应用程序对象
CSample1App theApp;


Generated at Tue Feb 9 15:09:56 2010 for Meizu M8 SDK Documentation by  doxygen 1.6.1