Mediaplayer.c
上传用户:hejie16899
上传日期:2021-10-21
资源大小:4758k
文件大小:31k
源码类别:

BREW编程

开发平台:

Visual C++

  1. /*===========================================================================
  2. FILE: MediaPlayer.c
  3. ===========================================================================*/
  4. /*===============================================================================
  5. INCLUDES AND VARIABLE DEFINITIONS
  6. =============================================================================== */
  7. #include "AEEModGen.h"          // Module interface definitions
  8. #include "AEEAppGen.h"          // Applet interface definitions
  9. #include "AEEShell.h"           // Shell interface definitions
  10. #include "AEEStdLib.h"
  11. #include "AEEFile.h" // File interface definitions
  12. #include "AEEMedia.h"
  13. //include of me
  14. #include "mediaplayer.h"
  15. #include "MediaPlayer.bid"
  16. #include "res.brh"
  17. /*-------------------------------------------------------------------
  18. /*-------------------------------------------------------------------
  19.             DEFINITIONS
  20. -------------------------------------------------------------------*/
  21. #define MP_LOGO_TIMER 2000 //开机logo暂停时间,2 seconds
  22. #define MP_ICONVIEWCTL_CY 20
  23. #define MP_VOLUME_STEP              25 //音量控制
  24. #define MP_SEEK_TIME                10000 //播放时快 进/退时间  10 seconds
  25. #define MP_PROGBAR_DX_DIV 3 // dx is dx/3 of main rectangle
  26. #define MP_PROGBAR_DY_DIV 2 // dy is dy/2 of main rectangle
  27. #define MP_PROGBAR_DX_OFFSET 4 // dx is reduced by offset
  28. #define MENU8_FT AEE_FT_NONE //边框类型,没有边框
  29. #define MENU8_SELECT_FT AEE_FT_RAISED //凸起的 2 像素三维边框
  30. //特定颜色的 SRC 像素是透明的意味着可看穿相应的 DST 像素
  31. #define MENU8_RO AEE_RO_TRANSPARENT
  32. #define MENU8_SELECT_RO AEE_RO_TRANSPARENT
  33. //掩码集 指示需要更改其颜色的项目
  34. #define MENU8_COLOR_MASK  (MC_BACK | MC_SEL_BACK | MC_SEL_TEXT)
  35. //设置显示颜色
  36. #define MENU8_BACKGROUND MAKE_RGB(255, 255, 204)
  37. #define MENU8_SELECT_BACKGROUND MAKE_RGB(153, 204, 204)
  38. #define MENU8_SELECT_TEXT RGB_BLACK
  39. #define MP_CLR_PROG_FILL MAKE_RGB(0, 128, 192)
  40. #define TB8_BACKGROUND MAKE_RGB(192,192,192)
  41. #define TB8_SELECT_BACKGROUND MAKE_RGB(192, 192, 192)
  42. #define MP_MEDIA_DIR "media" //保存歌曲目录
  43. #define MP_RELEASEIF(p)       MP_FreeIF((IBase **)&(p)) //释放接口指针,并赋值NULL
  44. /*-------------------------------------------------------------------
  45. Function Prototypes
  46. -------------------------------------------------------------------*/
  47. static  boolean MediaPlayer_HandleEvent(MediaPlayer* pMe, AEEEvent eCode, 
  48.                                              uint16 wParam, uint32 dwParam);
  49. boolean MediaPlayer_InitAppData(MediaPlayer* pMe);
  50. void    MediaPlayer_FreeAppData(MediaPlayer* pMe);
  51. /*===============================================================================
  52. FUNCTION DEFINITIONS
  53. =============================================================================== */
  54. static void MediaPlayer_DrawLogo (MediaPlayer* pMe);  //画开机LOGO
  55. static boolean MediaPlayer_SetWindow(MediaPlayer * pMe, CMPWindow eWin);//设置界面切换接口
  56. static void MediaPlayer_DrawMain (MediaPlayer* pMe);  //系统开始主界面
  57. static void MediaPlayer_DrawSongList (MediaPlayer* pMe);  //歌曲列表界面
  58. static void MediaPlayer_DrawSongPlay (MediaPlayer* pMe);  //歌曲播放界面
  59. //获取图片信息并判断是否居中显示
  60. static void MP_DrawImage(IImage * pImage, AEERect * pRect, boolean bCenter);
  61. static void     MP_PlaySong (MediaPlayer* pMe);  //播放歌曲
  62. static void MP_FreeIF(IBase ** ppif);  //释放接口指针并负责NULL
  63. static void MP_LoadFileToList(MediaPlayer *pMe);  //枚举目录歌曲列表
  64. static boolean MP_AddMenuItem(IMenuCtl * pMenu, uint16 wTextID, 
  65.    AECHAR * pText, uint16 wImageID, 
  66.    uint16 wItemID, uint32 dwData);  //设置菜单项
  67. static char * MP_GetFileName(const char * psz);  //取出文件名字,去掉目录
  68. static void MP_SetMenuAttr(IMenuCtl * pMenu, AEECLSID clsMenu, 
  69.    uint16 nColorDepth, AEERect * pRect, 
  70.    uint32 dwProps);
  71. static void MP_MediaNotify(void * pUser, AEEMediaCmdNotify * pCmdNotify);//media回调事情处理
  72. static void MP_UpdateProgCtl(MediaPlayer * pme, int nCmd, int nSubCmd, uint16 wResID);
  73. static void MP_FitStaticText(IDisplay * pd, IStatic * ps, AEEFont font, AECHAR * pszText);
  74. static void MP_FrameRect(IDisplay * pd, AEERect * pRect);
  75. static boolean  MP_IsPause(MediaPlayer * pMe);
  76. static boolean  CProgCtl_Init(MediaPlayer * pme, AEERect * pRectMain);
  77. static void CProgCtl_SetPos(MediaPlayer * pme, AECHAR * psz, uint16 wPct);
  78. static void CProgCtl_DrawHist(MediaPlayer * pme, uint16 wPct);
  79. /*===========================================================================
  80. FUNCTION: AEEClsCreateInstance
  81. ===========================================================================*/
  82. int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
  83. {
  84. *ppObj = NULL;
  85. if( ClsId == AEECLSID_MEDIAPLAYER )
  86. {
  87. // Create the applet and make room for the applet structure
  88. if( AEEApplet_New(sizeof(MediaPlayer),
  89.                           ClsId,
  90.                           pIShell,
  91.                           po,
  92.                           (IApplet**)ppObj,
  93.                           (AEEHANDLER)MediaPlayer_HandleEvent,
  94.                           (PFNFREEAPPDATA)MediaPlayer_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
  95.                           
  96. {
  97. //Initialize applet data, this is called before sending EVT_APP_START
  98.             // to the HandleEvent function
  99. if(MediaPlayer_InitAppData((MediaPlayer*)*ppObj))
  100. {
  101. //Data initialized successfully
  102. return(AEE_SUCCESS);
  103. }
  104. else
  105. {
  106. //Release the applet. This will free the memory allocated for the applet when
  107. // AEEApplet_New was called.
  108. IAPPLET_Release((IApplet*)*ppObj);
  109. return EFAILED;
  110. }
  111.         } // end AEEApplet_New
  112.     }
  113. return(EFAILED);
  114. }
  115. /*===========================================================================
  116. FUNCTION SampleAppWizard_HandleEvent
  117. ===========================================================================*/
  118. static boolean MediaPlayer_HandleEvent(MediaPlayer* pApp, AEEEvent eCode, 
  119.    uint16 wParam, uint32 dwParam)
  120. {  
  121. if (pApp->m_pMainMenu && IMENUCTL_HandleEvent(pApp->m_pMainMenu, eCode, wParam, dwParam))
  122. {
  123. return TRUE;
  124. }
  125. if (pApp->m_pFileMenu && IMENUCTL_HandleEvent(pApp->m_pFileMenu, eCode, wParam, dwParam))
  126. {
  127. pApp->m_item=IMENUCTL_GetSel(pApp->m_pFileMenu);
  128. return TRUE;
  129. }
  130. if (pApp->m_pPlayerMenu && IMENUCTL_HandleEvent(pApp->m_pPlayerMenu, eCode, wParam, dwParam))
  131. {
  132. return TRUE;
  133. }
  134.     switch (eCode) 
  135. {
  136.         // App is told it is starting up
  137.         case EVT_APP_START:                        
  138. MediaPlayer_DrawLogo(pApp); //画开机LOGO
  139.             return(TRUE);
  140.         // App is told it is exiting
  141.         case EVT_APP_STOP:
  142.             // Add your code here...
  143.        return(TRUE);
  144.  
  145.         case EVT_APP_SUSPEND:
  146.     // Add your code here...
  147.        return(TRUE);
  148.         // App is being resumed
  149.         case EVT_APP_RESUME:
  150.     // Add your code here...
  151.        return(TRUE);
  152.        
  153.         case EVT_APP_MESSAGE:
  154.     // Add your code here...
  155.        return(TRUE);
  156.         case EVT_KEY:
  157. //在歌曲列表界面并按了CLR键,退回到主界面,如果media_play,歌曲不停止 
  158. if ((pApp->m_eActiveWin==MPW_SONGLIST))
  159. {
  160. if (wParam == AVK_RIGHT && pApp->m_pIMedia)
  161. {
  162. MediaPlayer_SetWindow(pApp,MPW_SONGPLAYER);
  163. }
  164. if (wParam == AVK_LEFT || wParam == AVK_CLR)
  165. {
  166. IMENUCTL_SetActive( pApp->m_pFileMenu, FALSE );
  167. IMENUCTL_Redraw(pApp->m_pFileMenu);
  168. MediaPlayer_SetWindow(pApp,MPW_MAIN);
  169. }
  170. }
  171.     // Add your code here..
  172. if (pApp->m_eActiveWin ==  MPW_SONGPLAYER)
  173. {
  174. if (wParam == AVK_CLR)
  175. {
  176. IMENUCTL_SetActive( pApp->m_pPlayerMenu, FALSE );
  177. IMENUCTL_Redraw(pApp->m_pPlayerMenu);
  178. MediaPlayer_SetWindow(pApp,MPW_SONGLIST);
  179. }
  180. }
  181.        return(TRUE);
  182. case EVT_COMMAND:
  183. if (pApp->m_item == wParam)
  184. {
  185. if (pApp->m_pIMedia)
  186. {
  187. IMEDIA_Stop(pApp->m_pIMedia);
  188. }
  189. MediaPlayer_SetWindow(pApp,MPW_SONGPLAYER);
  190. MP_PlaySong(pApp);
  191. }
  192. switch(wParam)
  193. {
  194.             case IDS_SONGLIST:
  195.                 MediaPlayer_SetWindow(pApp,MPW_SONGLIST);
  196.                 break;
  197.             case IDM_PM_PLAY:
  198.                 if (MP_IsPause(pApp))
  199.                 {
  200.                     IMEDIA_Resume(pApp->m_pIMedia);
  201.                 }
  202.                 else if (SUCCESS != IMEDIA_GetTotalTime(pApp->m_pIMedia))
  203.                 {
  204.                     if (AEE_SUCCESS == IMEDIA_Play(pApp->m_pIMedia))
  205.                     {
  206.                         pApp->m_bPlayRec = TRUE;
  207.                     }
  208.                 }
  209.                 break;
  210.                 
  211.             case IDM_PM_STOP:
  212.                 IMEDIA_Stop(pApp->m_pIMedia);
  213.                 break;
  214. case IDM_PM_BACK:
  215. pApp->m_item--;
  216. IMEDIA_Stop(pApp->m_pIMedia);
  217. if (pApp->m_item < IDM_FILELIST_BASE)
  218. {
  219. pApp->m_item = pApp->m_MaxSongFile;
  220. }
  221. MediaPlayer_SetWindow(pApp,MPW_SONGPLAYER);
  222. MP_PlaySong(pApp);
  223.  break;
  224.                 
  225.             case IDM_PM_REWIND:
  226.                 IMEDIA_Rewind(pApp->m_pIMedia, MP_SEEK_TIME);
  227.                 break;
  228.                 
  229.             case IDM_PM_FF:
  230.                 IMEDIA_FastForward(pApp->m_pIMedia, MP_SEEK_TIME);
  231.                 break;
  232.                 
  233.             case IDM_PM_PAUSE:
  234.                 if (MP_IsPause(pApp))
  235.                     IMEDIA_Resume(pApp->m_pIMedia);
  236.                 else 
  237.                     IMEDIA_Pause(pApp->m_pIMedia);
  238.                 break;
  239. case IDM_PM_NEXT:
  240. pApp->m_item++;
  241. IMEDIA_Stop(pApp->m_pIMedia);
  242. if (pApp->m_item > pApp->m_MaxSongFile)
  243. {
  244. pApp->m_item = IDM_FILELIST_BASE;
  245. }
  246. MediaPlayer_SetWindow(pApp,MPW_SONGPLAYER);
  247. MP_PlaySong(pApp);
  248. break;
  249.             case IDM_PM_UP:
  250.                 {
  251.                     
  252.                     uint16   wVol = pApp->m_wVolume + MP_VOLUME_STEP;
  253.                     wVol = (wVol > AEE_MAX_VOLUME) ? AEE_MAX_VOLUME : wVol;
  254.                     IMEDIA_SetVolume(pApp->m_pIMedia, wVol);
  255.                     break;
  256.                 }
  257.                 
  258.             case IDM_PM_DOWN:
  259.                 {
  260.                     int16    nVol = (int16)pApp->m_wVolume - MP_VOLUME_STEP;
  261.                     nVol = (nVol < 0) ? 0 : nVol;
  262.                     IMEDIA_SetVolume(pApp->m_pIMedia, (uint16)nVol);
  263.                     break;
  264.                 }
  265.             default:
  266.                 break;
  267.             }
  268.             
  269.             default:
  270.                 break;
  271.     }
  272.     
  273.     return FALSE;
  274. }
  275. // this function is called when your application is starting up
  276. boolean MediaPlayer_InitAppData(MediaPlayer* pApp)
  277. {
  278.     // Get the device information for this handset.
  279.     // Reference all the data by looking at the pMe->DeviceInfo structure
  280.     // Check the API reference guide for all the handy device info you can get
  281.     pApp->DeviceInfo.wStructSize = sizeof(pApp->DeviceInfo);
  282.     ISHELL_GetDeviceInfo(pApp->a.m_pIShell,&pApp->DeviceInfo);
  283. pApp->m_cxWidth = pApp->DeviceInfo.cxScreen;
  284. pApp->m_cyHeight = pApp ->DeviceInfo.cyScreen;
  285. pApp->m_nColorDepth = pApp->DeviceInfo.nColorDepth;
  286.     return TRUE;
  287. }
  288. // this function is called when your application is exiting
  289. void MediaPlayer_FreeAppData(MediaPlayer* pMe)
  290. {
  291. int i;
  292. MP_RELEASEIF(pMe->m_pIMedia);
  293. MP_RELEASEIF(pMe->m_pTitle);
  294. MP_RELEASEIF(pMe->m_pFileMenu);
  295. MP_RELEASEIF(pMe->m_pPlayerMenu);
  296. MP_RELEASEIF(pMe->m_pSOFTKEYMenu);
  297. MP_RELEASEIF(pMe->m_pMainMenu);
  298.     for (i = 0; i < pMe->m_NumFiles; i++ )
  299. {
  300.     if (pMe->m_szFileArray[i])
  301.             FREE((void *)pMe->m_szFileArray[i]);
  302. }
  303. }
  304. //画开机LOGO
  305. static void MediaPlayer_DrawLogo(MediaPlayer* pApp)   
  306. //判断界面标记m_eActiveWin是否激活主界面
  307.     if (pApp->m_eActiveWin == MPW_MAIN)
  308.     {
  309.         MediaPlayer_SetWindow(pApp, MPW_MAIN); //进入界面选择,播放器开始主界面
  310.         return;
  311.     }
  312.     
  313.     {
  314. //装载资源编辑器logo图片
  315.         IImage *pi = ISHELL_LoadResImage(pApp->a.m_pIShell, RES_RES_FILE,IDI_LOGO);
  316. //转载成功就进行绘制,失败则释放IImage 指针并赋值NULL
  317.         if (pi)
  318.         {
  319.             AEERect rect;          
  320.             IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
  321.             SETAEERECT(&rect, 0, 0, pApp->m_cxWidth,pApp->m_cyHeight);
  322.             MP_DrawImage(pi, &rect, TRUE);
  323.             IDISPLAY_Update(pApp->a.m_pIDisplay);
  324.             MP_RELEASEIF(pi);
  325.         }
  326. else
  327. {
  328. MP_RELEASEIF(pi);
  329. }
  330.         pApp->m_eActiveWin = MPW_MAIN; //激活主界面标记m_eActiveWin = MPW_MAIN
  331. //计时器,LOGO停留MP_lOGO_TIMER时间,重新调用MediaPlayer_DrawLogo
  332.         ISHELL_SetTimer(pApp->a.m_pIShell, MP_LOGO_TIMER, (PFNNOTIFY)MediaPlayer_DrawLogo, pApp);
  333.     }    
  334. }
  335. //根据m_eActiveWin标记来选择界面
  336. static boolean MediaPlayer_SetWindow(MediaPlayer * pApp, CMPWindow eWin)
  337. {
  338.     DBGPRINTF("CMediaPlay_SetWindow");
  339.     switch (eWin)
  340.     {
  341.     case MPW_MAIN:
  342.         MediaPlayer_DrawMain(pApp); //播放器开始主界面
  343.         break;
  344.         
  345.     case MPW_SONGLIST:   
  346.         MediaPlayer_DrawSongList(pApp); //歌曲列表界面
  347.         break;
  348.         
  349.     case MPW_SONGPLAYER:     
  350.         MediaPlayer_DrawSongPlay(pApp); //歌曲播放界面
  351.         break;
  352.     
  353. default:             
  354.         return FALSE; 
  355.         break;
  356.     }
  357.     return TRUE;    
  358. }
  359. //系统开始主界面
  360. static void MediaPlayer_DrawMain (MediaPlayer* pApp)
  361. {
  362. AEERect rect;
  363. AEERect rRect; 
  364. IImage *pi = ISHELL_LoadResImage(pApp->a.m_pIShell, RES_RES_FILE, IDI_MAINLOGO); 
  365.     if (!pi)
  366.     {   
  367. MP_RELEASEIF(pApp);   
  368. MP_RELEASEIF(pi);
  369. return;
  370.     }
  371.     pApp->m_eActiveWin = MPW_MAIN;
  372. IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
  373. if (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_MENUCTL, (void **)&pApp->m_pMainMenu))
  374.     {
  375. MP_RELEASEIF(pApp);  
  376. return;
  377.     } 
  378. SETAEERECT(&rect, 0, 0, pApp->m_cxWidth,pApp->m_cyHeight/2);
  379. SETAEERECT(&rRect, 0, pApp->m_cyHeight/2, pApp->m_cxWidth,pApp->m_cyHeight/2);
  380. MP_DrawImage(pi, &rect, TRUE);
  381. IDISPLAY_Update(pApp->a.m_pIDisplay);
  382. MP_RELEASEIF(pi);
  383. //设置菜单 
  384. IMENUCTL_AddItem(pApp->m_pMainMenu,RES_RES_FILE, IDS_SONGLIST,IDS_SONGLIST, NULL, 0 ); 
  385. IMENUCTL_AddItem(pApp->m_pMainMenu,RES_RES_FILE, IDS_ABOUT,IDS_ABOUT, NULL, 0 ); 
  386. IMENUCTL_SetRect( pApp->m_pMainMenu, &rRect ); //设置菜单位置 
  387. IMENUCTL_SetActive( pApp->m_pMainMenu, TRUE ); //激活主菜单控件
  388. }
  389. //歌曲列表界面      
  390. static void MediaPlayer_DrawSongList (MediaPlayer* pApp)  
  391. {
  392. IImage * pHeader;
  393. AEERect rect;
  394. pApp->m_eActiveWin = MPW_SONGLIST; //设置歌曲列表界面标记
  395. DBGPRINTF("CMediaPlay_DrawSongList");  
  396. IMENUCTL_SetActive( pApp->m_pMainMenu, FALSE);  //主菜单控件
  397. IMENUCTL_Redraw(pApp->m_pMainMenu);
  398. IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
  399. pHeader = ISHELL_LoadResImage(pApp->a.m_pIShell,RES_RES_FILE,IDI_HEADER);
  400. if (!pHeader)
  401. {
  402. MP_RELEASEIF(pHeader);
  403. }
  404. SETAEERECT(&rect,0,0,pApp->m_cxWidth, pApp->m_cyHeight/7);
  405. MP_DrawImage(pHeader, &rect, TRUE);
  406. IDISPLAY_Update(pApp->a.m_pIDisplay);
  407. MP_RELEASEIF(pHeader);
  408.     MP_LoadFileToList(pApp);//装入歌曲菜单 
  409. }
  410. //歌曲播放界面
  411. static void MediaPlayer_DrawSongPlay (MediaPlayer* pApp)  
  412. {
  413. AEERect  rect;
  414. AEERect  rectMenu;
  415. AEERect  rectProg;
  416. IImage   *pi;
  417. IImage   *pHeader;
  418. pi = ISHELL_LoadResImage(pApp->a.m_pIShell, RES_RES_FILE, IDI_MAINLOGO);
  419. if (!pi)
  420. {
  421. MP_RELEASEIF(pi);
  422. }
  423. pHeader = ISHELL_LoadResImage(pApp->a.m_pIShell,RES_RES_FILE,IDI_HEADER);
  424. if (!pHeader)
  425. {
  426. MP_RELEASEIF(pHeader);
  427. }
  428. pApp->m_eActiveWin = MPW_SONGPLAYER;
  429. IMENUCTL_SetActive( pApp->m_pFileMenu, FALSE);  //主菜单控件失效
  430. IMENUCTL_Redraw(pApp->m_pFileMenu);
  431. IDISPLAY_ClearScreen(pApp->a.m_pIDisplay); 
  432. {
  433. SETAEERECT(&rect,0,0,pApp->m_cxWidth, pApp->m_cyHeight/7);
  434. MP_DrawImage(pHeader, &rect, TRUE);
  435. }
  436. {
  437.         SETAEERECT(&rect, 0, pApp->m_cyHeight/7, pApp->m_cxWidth,pApp->m_cyHeight/2);
  438.         MP_DrawImage(pi, &rect, TRUE);   
  439.     }
  440. IDISPLAY_Update(pApp->a.m_pIDisplay);
  441. MP_RELEASEIF(pHeader);
  442. MP_RELEASEIF(pi);
  443.     {
  444. int cx = pApp->m_cxWidth;
  445. int cy = pApp->m_cyHeight;
  446. int y = cy - MP_ICONVIEWCTL_CY;
  447. int dy = MP_ICONVIEWCTL_CY;
  448. SETAEERECT(&rectMenu, 0, y, cx, dy);
  449.         ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_SOFTKEYCTL, 
  450. (void**)(&pApp->m_pPlayerMenu));
  451. SETAEERECT(&rectProg, 
  452. 0,
  453. pApp->m_cyHeight-pApp->m_cyHeight/7, 
  454. pApp->m_cxWidth, 
  455. pApp->m_cyHeight/5-pApp->m_cyHeight/7);
  456. if (!CProgCtl_Init(pApp, &rectProg))
  457. {
  458. }
  459.         MP_SetMenuAttr(pApp->m_pPlayerMenu,
  460.    AEECLSID_ICONVIEWCTL, 
  461.    pApp->m_nColorDepth, 
  462.    &rectMenu, 
  463.    MP_MAXSOFTKEYITEMS);
  464. MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_PLAY,     IDM_PM_PLAY,   0);
  465.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_PAUSE,    IDM_PM_PAUSE,   0);
  466.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_STOP,    IDM_PM_STOP, 0);
  467.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_BAKE,     IDM_PM_BACK, 0);
  468.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_REWIND,   IDM_PM_REWIND,  0);
  469.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_FASTFORWARD, IDM_PM_FF,   0);
  470.         MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_NEXT,        IDM_PM_NEXT,     0);
  471. MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_UP,          IDM_PM_UP,     0);
  472. MP_AddMenuItem(pApp->m_pPlayerMenu, 0, NULL, IDI_DOWN,        IDM_PM_DOWN,     0);
  473.         IMENUCTL_SetRect( pApp->m_pPlayerMenu, &rectMenu );
  474.         IMENUCTL_SetActive( pApp->m_pPlayerMenu, TRUE );
  475.     }
  476. }
  477. //播放歌曲
  478. static void MP_PlaySong(MediaPlayer* pApp)
  479. {
  480. AEEMediaData  pmd;
  481. char *         pszBuf;
  482. if(SUCCESS != ISHELL_CreateInstance(pApp->a.m_pIShell, 
  483. AEECLSID_MEDIAMP3, 
  484. (void **)&pApp->m_pIMedia))
  485. {
  486. MP_RELEASEIF(pApp->m_pIMedia);
  487. }
  488. pszBuf = pApp->m_szFileArray[pApp->m_item - IDM_FILELIST_BASE];
  489.     if (!pszBuf)
  490. {
  491. return;
  492. }
  493. pmd.clsData = MMD_FILE_NAME;
  494. pmd.pData = pszBuf;
  495. pmd.dwSize = 0;
  496. IMEDIA_SetMediaData (pApp->m_pIMedia, &pmd);
  497. IMEDIA_GetTotalTime(pApp->m_pIMedia);
  498. IMEDIA_Play(pApp->m_pIMedia);
  499. if (SUCCESS == IMEDIA_RegisterNotify(pApp->m_pIMedia, MP_MediaNotify ,pApp))
  500. {
  501. }
  502. }
  503. //获取图片信息并判断是否居中显示
  504. static void MP_DrawImage(IImage * pImage, AEERect * pRect, boolean bCenter)
  505. {
  506. AEEImageInfo   ii;
  507.     int            x;
  508.     int            y;
  509.     
  510.     DBGPRINTF("MP_DrawImage");
  511.     IIMAGE_GetInfo(pImage, &ii);
  512.     
  513.     // Do not display if image does not fit in the allocated rectangle.
  514.     if (ii.cx > pRect->dx || ii.cy > pRect->dy)
  515. {
  516.         return;
  517.     }
  518.     if (bCenter)
  519.     {
  520.         x = pRect->x + (pRect->dx / 2) - (ii.cxFrame / 2);
  521.         y = pRect->y + (pRect->dy / 2) - (ii.cy / 2);
  522.     }
  523.     else
  524.     {
  525.         x = pRect->x;
  526.         y = pRect->y;
  527.     }
  528.     
  529.     IIMAGE_Start(pImage, x, y);
  530. }
  531. //释放接口指针并负责NULL
  532. static void MP_FreeIF(IBase ** ppif)
  533. {
  534.     if (ppif && *ppif)
  535.     {
  536.         IBASE_Release(*ppif);
  537.         *ppif = NULL;
  538.     }
  539. }
  540. //装在歌曲文件到播放列表
  541. static void MP_LoadFileToList(MediaPlayer *pApp)
  542. {
  543. IFileMgr*  pIFileMgr;
  544.     uint16      wItemID = 0;
  545.     AECHAR*     pzBuf;
  546. // char*      pszBuf;
  547.     AEERect     rect;
  548.     
  549.     SETAEERECT(&rect,
  550. 0, 
  551. pApp->m_cyHeight/7, 
  552. pApp->m_cxWidth, 
  553. pApp->m_cyHeight-pApp->m_cyHeight/7);
  554. pzBuf = MALLOC(MAX_FILE_NAME * sizeof(AECHAR));    
  555. if(!pzBuf)
  556.     {
  557.         MP_RELEASEIF(pApp);
  558.         return ;
  559.     }
  560.     
  561.     if (ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_MENUCTL, (void **)&pApp->m_pFileMenu))
  562.     {
  563.         MP_RELEASEIF(pApp);
  564.     }
  565.     if ( ISHELL_CreateInstance(pApp->a.m_pIShell, AEECLSID_FILEMGR, (void **)&pIFileMgr) != SUCCESS )
  566.     {
  567.         MP_RELEASEIF(pApp);
  568.         return ;
  569.     }
  570.     
  571.     IFILEMGR_EnumInit(pIFileMgr, MP_MEDIA_DIR, FALSE);
  572.     while (wItemID < CMP_MAX_FILES && IFILEMGR_EnumNext(pIFileMgr, &pApp->m_pFileInfo))
  573.     {
  574.         char *   szName;
  575.         
  576.         //pApp->m_szFileArray[wItemID] = STRDUP(pApp->m_pFileInfo.szName);
  577.         pApp->m_szFileArray[wItemID] = (char*)MALLOC(STRLEN(pApp->m_pFileInfo.szName) + 1);
  578. MEMSET(pApp->m_szFileArray[wItemID], 0, STRLEN(pApp->m_pFileInfo.szName) + 1);
  579. STRCPY(pApp->m_szFileArray[wItemID], pApp->m_pFileInfo.szName);
  580.         if (!pApp->m_szFileArray[wItemID])
  581.         {
  582.             pApp->m_NumFiles = wItemID;
  583.             FREE(pzBuf);
  584.             MP_RELEASEIF(pIFileMgr);
  585.         }
  586.         
  587.         szName = MP_GetFileName(pApp->m_pFileInfo.szName);
  588.         if (szName)
  589.         {
  590.             STRTOWSTR(szName, pzBuf, MAX_FILE_NAME);
  591.             MP_AddMenuItem(pApp->m_pFileMenu, 
  592. 0, 
  593. pzBuf, 
  594. IDI_IMAGE2, 
  595. (uint16)(IDM_FILELIST_BASE + wItemID),
  596. 0); 
  597.         }
  598.         
  599.         wItemID++;
  600.     }
  601. pApp->m_MaxSongFile = IDM_FILELIST_BASE+wItemID-1;
  602. pApp->m_NumFiles = wItemID;
  603.     IMENUCTL_SetRect( pApp->m_pFileMenu, &rect );
  604.     IMENUCTL_SetActive( pApp->m_pFileMenu, TRUE );
  605.     IMENUCTL_Redraw(pApp->m_pFileMenu);
  606.     MP_RELEASEIF(pIFileMgr);
  607.     FREE(pzBuf);
  608. }
  609. static boolean MP_AddMenuItem(IMenuCtl * pMenu, uint16 wTextID,                  
  610.                                AECHAR * pText, uint16 wImageID, 
  611.                                uint16 wItemID, uint32 dwData)
  612. {
  613. CtlAddItem  ai;
  614.     
  615.     // Fill in the CtlAddItem structure values
  616.     ai.pText = pText;
  617.     ai.pImage = NULL;
  618.     ai.pszResImage = RES_RES_FILE;
  619.     ai.pszResText = RES_RES_FILE;
  620.     ai.wText = wTextID;
  621.     ai.wFont = AEE_FONT_NORMAL;
  622.     ai.wImage = wImageID;
  623.     ai.wItemID = wItemID;
  624.     ai.dwData = dwData;   
  625.     // Add the item to the menu control
  626.     return IMENUCTL_AddItemEx( pMenu, &ai );
  627. }
  628. static char * MP_GetFileName(const char * psz)
  629. {
  630. char *   pszName = STRRCHR(psz, (int)DIRECTORY_CHAR);
  631.     
  632.     if (pszName)
  633. {
  634.         pszName++;
  635.     }
  636. else
  637. {
  638.         pszName = (char *)psz;
  639.     }
  640.     return pszName;
  641. }
  642. static void MP_SetMenuAttr(IMenuCtl * pMenu, AEECLSID clsMenu, uint16 nColorDepth, 
  643.    AEERect * pRect, uint32 dwProps)
  644. {
  645. AEEItemStyle sel, normal;
  646. AEEMenuColors col;
  647. // Menu Style
  648. normal.ft = MENU8_FT;
  649. normal.xOffset = 0;
  650. normal.yOffset = 0;
  651. normal.roImage = MENU8_RO;
  652.     sel.ft = MENU8_SELECT_FT;
  653. sel.xOffset = 0;
  654. sel.yOffset = 0;
  655. sel.roImage = MENU8_SELECT_RO;
  656. // Menu Colors
  657.     col.cSelText = MENU8_SELECT_TEXT;
  658. col.wMask = MENU8_COLOR_MASK;
  659. if (clsMenu == AEECLSID_MENUCTL)
  660. {
  661. col.cBack = MENU8_BACKGROUND;
  662. col.cSelBack = MENU8_SELECT_BACKGROUND;
  663. dwProps |= IMENUCTL_GetProperties(pMenu);
  664. }
  665. else if (clsMenu == AEECLSID_SOFTKEYCTL || clsMenu == AEECLSID_ICONVIEWCTL)
  666. {
  667. col.cBack = TB8_BACKGROUND;
  668. col.cSelBack = TB8_SELECT_BACKGROUND;
  669. dwProps |= MP_ICON_TEXT_TOP | MP_NO_ARROWS;
  670. }
  671. if (clsMenu == AEECLSID_MENUCTL 
  672. || clsMenu == AEECLSID_SOFTKEYCTL 
  673. || clsMenu == AEECLSID_ICONVIEWCTL)
  674. {
  675. IMENUCTL_SetStyle(pMenu, &normal, &sel);
  676. IMENUCTL_SetColors(pMenu, &col);
  677. IMENUCTL_SetProperties(pMenu, dwProps);
  678. if (pRect)
  679. {
  680. IMENUCTL_SetRect(pMenu, pRect);
  681. }
  682. }
  683. }
  684. static void MP_MediaNotify(void * pUser, AEEMediaCmdNotify * pCmdNotify)
  685. {
  686.     uint16         nTextID = 0;
  687.     MediaPlayer* pMe=(MediaPlayer *)pUser;
  688.     
  689.     switch (pCmdNotify->nStatus)
  690.     {
  691.     case MM_STATUS_SEEK_FAIL:
  692.     case MM_STATUS_PAUSE_FAIL:
  693.     case MM_STATUS_RESUME_FAIL:
  694.         nTextID = IDS_ERR_PLAYCTL_CMD;
  695.         break;
  696.     }
  697.     
  698.     if (pCmdNotify->nCmd == MM_CMD_PLAY )  // IMEDIA_Play/IMEDIA_Record events
  699.     { 
  700.         nTextID = IDS_PLAY ;
  701.         switch (pCmdNotify->nStatus)
  702.         {
  703.         case MM_STATUS_START:
  704.             IMENUCTL_SetSel(pMe->m_pPlayerMenu, IDM_PM_STOP);
  705.             break;
  706.             
  707.         case MM_STATUS_MEDIA_SPEC:
  708.             break;
  709.             
  710.         case MM_STATUS_TICK_UPDATE:  // Typcally, one-second update
  711.             pMe->m_dwPlayPos++;
  712.             
  713. if (!pMe->m_dwTotalTime) // If no total time increase by 20!
  714. pMe->m_wPct = (pMe->m_wPct + 20) % 100;
  715. }
  716.             break; 
  717.             
  718.         case MM_STATUS_SEEK:
  719.             nTextID = IDS_SEEK;
  720.             
  721. if (pCmdNotify->dwSize)
  722. {
  723. pMe->m_dwPlayPos = (uint32)pCmdNotify->pCmdData / 1000;
  724. }
  725.             break;
  726.             
  727.         case MM_STATUS_PAUSE:
  728.             nTextID = IDS_PAUSE;
  729.             if (pCmdNotify->dwSize)
  730. pMe->m_dwPlayPos = (uint32)pCmdNotify->pCmdData / 1000;
  731.             }
  732. break;
  733.             
  734.         case MM_STATUS_RESUME:
  735.             
  736. if (pCmdNotify->dwSize)
  737. {
  738. pMe->m_dwPlayPos = (uint32)pCmdNotify->pCmdData / 1000;
  739.             }
  740. break;
  741.             
  742.         case MM_STATUS_DONE:    // playback done
  743.         case MM_STATUS_ABORT:   // playback aborted
  744.             {
  745.                 nTextID = (pCmdNotify->nStatus == MM_STATUS_DONE) ? IDS_STOP : IDS_ABORT;
  746.                 pMe->m_bPlayRec = FALSE;
  747.                 pMe->m_dwPlayPos = 0;
  748.                 pMe->m_wPct = 0;               
  749.                 IMENUCTL_SetSel(pMe->m_pPlayerMenu, IDM_PM_PLAY);         
  750.                 break;
  751.             }
  752.         }
  753.         
  754. if (pMe->m_eActiveWin == MPW_SONGPLAYER)
  755. {
  756. MP_UpdateProgCtl(pMe, pCmdNotify->nCmd, pCmdNotify->nSubCmd, nTextID);
  757. }
  758.         
  759.     } // MM_CMD_PLAY
  760.     else if (pCmdNotify->nCmd == MM_CMD_GETTOTALTIME)
  761.     {
  762.         if (pCmdNotify->nStatus == MM_STATUS_DONE || pCmdNotify->nStatus == MM_STATUS_ABORT)
  763.         {
  764.             pMe->m_dwTotalTime = ((uint32)pCmdNotify->pCmdData) / 1000;
  765.             if (AEE_SUCCESS == IMEDIA_Play(pMe->m_pIMedia))
  766. {
  767. pMe->m_bPlayRec = TRUE;
  768. }
  769. if (pMe->m_eActiveWin == MPW_SONGPLAYER)
  770. {
  771. MP_UpdateProgCtl(pMe, pCmdNotify->nCmd, pCmdNotify->nSubCmd, nTextID);
  772. }
  773.         }
  774.     } // MM_CMD_GETTOTALTIME
  775.     else if (pCmdNotify->nCmd == MM_CMD_SETMEDIAPARM)
  776.     {
  777.         if (pCmdNotify->nSubCmd == MM_PARM_VOLUME && pCmdNotify->nStatus == MM_STATUS_DONE)
  778.         {
  779.             IMEDIA_GetMediaParm(pMe->m_pIMedia, MM_PARM_VOLUME, NULL, NULL);
  780.             return;
  781.         }
  782.     } // MM_CMD_SETMEDIAPARM
  783.     else if (pCmdNotify->nCmd == MM_CMD_GETMEDIAPARM) // IMEDIA_GetMediaParm() events
  784.     {
  785.         if (pCmdNotify->nSubCmd == MM_PARM_VOLUME && pCmdNotify->nStatus == MM_STATUS_DONE)
  786.         {
  787.             pMe->m_wVolume = (uint16)(uint32)pCmdNotify->pCmdData;
  788.             pMe->m_bProgTimer = FALSE;
  789. if (pMe->m_eActiveWin == MPW_SONGPLAYER)
  790. {
  791. MP_UpdateProgCtl(pMe, pCmdNotify->nCmd, pCmdNotify->nSubCmd, IDS_VOLUME);
  792. }
  793.             
  794.             pMe->m_bProgTimer = TRUE;
  795.         }
  796.     }  // MM_CMD_GETMEDIAPARM
  797. }
  798. static boolean MP_IsPause(MediaPlayer * pApp)
  799. {
  800.     boolean  bChg;
  801.     int      nState = IMEDIA_GetState(pApp->m_pIMedia, &bChg);
  802.     
  803.     if (bChg)
  804. {
  805.         return FALSE;
  806.     }
  807.     return (nState == MM_STATE_PLAY_PAUSE);
  808. }
  809. static void MP_UpdateProgCtl(MediaPlayer * pme, int nCmd, int nSubCmd, uint16 wResID)
  810. {
  811.    char     sz[32];
  812.    AECHAR   szBuf[32];
  813.    
  814.    if ( !ISHELL_LoadResString(pme->a.m_pIShell, 
  815.    RES_RES_FILE, wResID, pme->m_szText, sizeof(pme->m_szText)) )
  816.    {
  817.       pme->m_szText[0] = (AECHAR)0;
  818.    }
  819.    if (nCmd == MM_CMD_PLAY)
  820.    {
  821.       SPRINTF(sz, 
  822.   " %02d:%02d/%02d:%02d", 
  823.   pme->m_dwPlayPos/60, 
  824.   pme->m_dwPlayPos%60, 
  825.   pme->m_dwTotalTime/60, 
  826.   pme->m_dwTotalTime%60);
  827.       STRTOWSTR(sz, szBuf, sizeof(szBuf));
  828.       WSTRCAT(pme->m_szText, szBuf);
  829.       if (pme->m_dwTotalTime)
  830.   {
  831.          pme->m_wPct = (uint16)((100 * pme->m_dwPlayPos) / pme->m_dwTotalTime);
  832.    
  833.   }
  834. }
  835.    else if (nCmd == MM_CMD_GETTOTALTIME)
  836.    {
  837.       SPRINTF(sz, 
  838.   " %02d:%02d/%02d:%02d", 
  839.   pme->m_dwPlayPos/60, 
  840.   pme->m_dwPlayPos%60, 
  841.   pme->m_dwTotalTime/60, 
  842.   pme->m_dwTotalTime%60);
  843.       STRTOWSTR(sz, szBuf, sizeof(szBuf));
  844.       WSTRCAT(pme->m_szText, szBuf);
  845.    }
  846.    else if (nCmd == MM_CMD_GETMEDIAPARM && nSubCmd == MM_PARM_VOLUME)
  847.    {
  848.       SPRINTF(sz, "[%u]", pme->m_wVolume);
  849.       STRTOWSTR(sz, szBuf, sizeof(szBuf));
  850.       WSTRCAT(pme->m_szText, szBuf);
  851.       pme->m_wPct = (100 * pme->m_wVolume) / AEE_MAX_VOLUME;
  852.    }
  853.    else
  854.    {
  855.       pme->m_wPct = 0;
  856.    }
  857.    CProgCtl_SetPos(pme, pme->m_szText, pme->m_wPct);
  858.    IDISPLAY_Update(pme->a.m_pIDisplay);
  859. }
  860. static void CProgCtl_SetPos(MediaPlayer * pme, AECHAR * psz, uint16 wPct)
  861. {
  862.    MP_FitStaticText(pme->a.m_pIDisplay, pme->m_pTitle, AEE_FONT_NORMAL, psz);
  863.    ISTATIC_Redraw(pme->m_pTitle);
  864.    CProgCtl_DrawHist(pme, wPct);
  865.    MP_FrameRect(pme->a.m_pIDisplay, &pme->m_rectMain);
  866. }
  867. static void MP_FitStaticText(IDisplay * pd, IStatic * ps, AEEFont font, AECHAR * pszText)
  868. {
  869.    int      nFits;
  870.    AEERect  rect;
  871.    int      nLen = WSTRLEN(pszText);
  872.    AECHAR   chSave = (AECHAR)0;
  873.    ISTATIC_GetRect(ps, &rect);
  874.    IDISPLAY_MeasureTextEx(pd, font, pszText, -1, rect.dx,  &nFits);
  875.    
  876.    if (nFits < nLen)
  877.    {
  878.       chSave = pszText[nFits];
  879.       pszText[nFits] = (AECHAR)0;
  880.    }
  881.    ISTATIC_SetText(ps, NULL, pszText, AEE_FONT_NORMAL, font);
  882.    
  883.    if (nFits < nLen)
  884.    {
  885.    pszText[nFits] = chSave;
  886.    }
  887. }
  888. static void CProgCtl_DrawHist(MediaPlayer * pme, uint16 wPct)
  889. {
  890.    AEERect  rc;
  891.    MEMCPY(&rc, &pme->m_rectBar, sizeof(AEERect));
  892.    if(wPct > 100)
  893.    {
  894.       wPct = 100;
  895.    }
  896.    IDISPLAY_DrawFrame(pme->a.m_pIDisplay, &rc, AEE_FT_RAISED, CLR_SYS_SCROLLBAR);
  897.    rc.dx = (rc.dx * wPct) / 100;
  898.    IDISPLAY_FillRect(pme->a.m_pIDisplay, &rc, MP_CLR_PROG_FILL);
  899. }
  900. static void MP_FrameRect(IDisplay * pd, AEERect * pRect)
  901. {
  902.    RGBVAL   clr = IDISPLAY_SetColor(pd, CLR_USER_FRAME, CLR_SYS_DK_SHADOW);
  903.    IDISPLAY_FrameRect(pd, pRect);
  904.    IDISPLAY_SetColor(pd, CLR_USER_FRAME, clr);
  905. }
  906. static boolean CProgCtl_Init(MediaPlayer * pme, AEERect * pRectMain)
  907. {
  908.    AEERect  rect;
  909.    int      x, y;
  910.    int      dxProg, dyProg;
  911.    MEMCPY(&pme->m_rectMain, pRectMain, sizeof(AEERect));
  912.    if (ISHELL_CreateInstance(pme->a.m_pIShell, AEECLSID_STATIC, (void **)&pme->m_pTitle))
  913.    {
  914.    return FALSE;
  915.    }
  916.    // ProgBar rect
  917.    dxProg = pRectMain->dx / MP_PROGBAR_DX_DIV;
  918.    dyProg = pRectMain->dy / MP_PROGBAR_DY_DIV;
  919.    x = pRectMain->x + pRectMain->dx - dxProg + 1;
  920.    y = pRectMain->y + pRectMain->dy / 2 - dyProg/2;
  921.    if (dxProg > MP_PROGBAR_DX_OFFSET)
  922.    {
  923.    dxProg -= MP_PROGBAR_DX_OFFSET;
  924.    }
  925.    SETAEERECT(&pme->m_rectBar, x, y, dxProg, dyProg);
  926.    SETAEERECT(&rect, pRectMain->x + 1, pRectMain->y + 1, pRectMain->dx - dxProg, pRectMain->dy);
  927.    ISTATIC_SetRect(pme->m_pTitle, &rect);
  928.    ISTATIC_SetProperties(pme->m_pTitle, ST_CENTERTEXT | ST_NOSCROLL);
  929.    return TRUE;
  930. }