为什么我在Windows mobile 5.0下编写的MP3播放器只能播放wav格式的歌曲囧,怎么才能播放MP3文件呢?
"playControl.h"
- C/C++ code
#pragma once#include <streams.h>#pragma comment (lib,"Ole32.lib")#pragma comment (lib,"Strmiids.lib")#define HELPER_RELEASE(x) { if (x) x->Release(); x = NULL; }#define WM_GRAPHNOTIFY WM_USER+1class CPlayControl{public: CPlayControl(void);public: ~CPlayControl(void);public: IGraphBuilder *pGB; //这是directshow的核心 IMediaControl *pMC; //帮我们连接filter(媒体文件,解码器等) //简单的说,它帮我们简单地打开和播放文件. IMediaEventEx *pME; //处理过滤器图表的事件 IVideoWindow *pVW; //用这个来控制directshow的视频窗口 IBasicAudio *pBA; //用于控制音频流的音量和平衡 IBasicVideo *pBV; //用于设置视频特性,如视频显示的目的区域和源区域 IMediaSeeking *pMS; //提供搜索数据流位置和设置播放速率的方法,播放位置 enum PLAYSTATE { RUNING, PAUSED, STOPPED, INIT };public: PLAYSTATE playControlInfo; HWND ghApp;public: bool SetNotifyWindow(HWND inWindow); bool Create(); bool RenderFile(WCHAR *szFile); bool buttonPlay(); bool buttonPause(); void buttonStop(); void CloseInterfaces(void); bool ListPlay(WCHAR *szFile); float GetAlltime(); float GetCurrentPosition(); bool SetCurrentPosition(double inPosition) ; bool Running(); void SetVol(int m_vol); IMediaEventEx * GetEventHandle();};
"playControl.cpp"
- C/C++ code
#include "StdAfx.h"#include "PlayControl.h"CPlayControl::CPlayControl(void){ IGraphBuilder *pGB = NULL; IMediaControl *pMC = NULL; IMediaEventEx *pME = NULL; IVideoWindow *pVW = NULL; IBasicAudio *pBA = NULL; IBasicVideo *pBV = NULL; IMediaSeeking *pMS = NULL; playControlInfo = STOPPED; ghApp = 0;}CPlayControl::~CPlayControl(void){ CoUninitialize();}void CPlayControl::CloseInterfaces(void){ HELPER_RELEASE(pMC); HELPER_RELEASE(pME); HELPER_RELEASE(pMS); HELPER_RELEASE(pBV); HELPER_RELEASE(pBA); HELPER_RELEASE(pVW); HELPER_RELEASE(pGB); // No current media state playControlInfo = INIT;}bool CPlayControl::Create(){ if(FAILED(CoInitialize(NULL))) return false; // Get the interface for DirectShow's GraphBuilder if( SUCCEEDED( CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGB ))) { HRESULT hr = NOERROR; // QueryInterface for DirectShow interfaces hr |= (pGB->QueryInterface(IID_IMediaControl, (void **)&pMC)); hr |= (pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME)); hr |= (pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS)); // Query for video interfaces, which may not be relevant for audio files hr |= (pGB->QueryInterface(IID_IVideoWindow, (void **)&pVW)); hr |= (pGB->QueryInterface(IID_IBasicVideo, (void **)&pBV)); // Query for audio interfaces, which may not be relevant for video-only files hr |= (pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA)); return true; } return false;}bool CPlayControl::RenderFile(WCHAR *szFile){ if ( pGB && SUCCEEDED( pGB->RenderFile(szFile, NULL))) { return true; } else return false ;}bool CPlayControl::SetNotifyWindow(HWND inWindow){ if (pME) { pME->SetNotifyWindow((OAHWND)inWindow, WM_GRAPHNOTIFY, 0); return true; } return false;}bool CPlayControl::Running(){ if(playControlInfo == RUNING || playControlInfo == PAUSED) return true; else return false;}bool CPlayControl::ListPlay(WCHAR *szFile){ if(pME) { pME->SetNotifyWindow(NULL,0,0); CloseInterfaces(); } if(playControlInfo == RUNING || playControlInfo == PAUSED) { playControlInfo = STOPPED; } if(Create() && RenderFile(szFile)) { pMC->Run(); playControlInfo = RUNING ; return true; } return false;}bool CPlayControl::buttonPlay(){ if(playControlInfo == RUNING) return FALSE; else { pMC->Run(); playControlInfo = RUNING; return TRUE; }}bool CPlayControl::buttonPause(){ if(playControlInfo == RUNING) { pMC->Pause(); playControlInfo = PAUSED; return TRUE; } else return FALSE;}void CPlayControl::buttonStop(){ if((playControlInfo == PAUSED) || (playControlInfo == RUNING)) { pMC->Stop(); } playControlInfo = STOPPED; pME->SetNotifyWindow(NULL,0,0); CloseInterfaces();}IMediaEventEx * CPlayControl::GetEventHandle(){ return pME;}bool CPlayControl::SetCurrentPosition(double inPosition){ if (pMS) { __int64 one = 10000000; __int64 position = (__int64)(one * inPosition); HRESULT hr = pMS->SetPositions(&position, AM_SEEKING_AbsolutePositioning | AM_SEEKING_SeekToKeyFrame, 0, AM_SEEKING_NoPositioning); return SUCCEEDED(hr); } return false;}void CPlayControl::SetVol(int m_vol){ pBA->put_Balance(0); pBA->put_Volume( -m_vol * 100);}float CPlayControl::GetAlltime(){ float time ; if(pMS) { LONGLONG allTimes ; if(SUCCEEDED(pMS->GetDuration(&allTimes))) { time = (float)allTimes / 10000000 ; return time; } } return 0 ;}float CPlayControl::GetCurrentPosition(){ float time ; if(pMS) { LONGLONG theTimes ; if(SUCCEEDED(pMS->GetCurrentPosition(&theTimes))) { time = (float)theTimes / 10000000 ; return time; } } return 0 ;}