Web/DB プログラミング徹底解説

ホーム > Windows 徹底解説 > MPEG3 の再生

MPEG3 の再生

スポンサーリンク

おそらく最も簡単な方法は DirectShow を利用することだと思います。

サンプルコードのダウンロード [makefile でのビルド, playmp.zip]

以下のコードは DirectShow を使って MP3 音楽ファイルを再生する方法を示しています。 COM を使わないといけないのでコードがこんな風になっちゃってます。

#define UNICODE
#define _UNICODE

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <assert.h>
#include <dshow.h>
#include <process.h>
#include "playmp.h"

#define IDM_BUTTON1 (100)
#define IDM_EDIT1 (101)


TCHAR g_lpszClassName[] = TEXT("_MY_PLAYMP3_");
HWND g_hWnd = NULL;
HWND g_hButton = NULL;
HWND g_hEdit = NULL;


//////////////////////////////////////////////////////////////////////////


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

     WNDCLASSEX wcl;

     wcl.cbSize = sizeof(WNDCLASSEX);
     wcl.hInstance = hInstance;
     wcl.lpszClassName = g_lpszClassName;
     wcl.lpfnWndProc = WndProc;
     wcl.style = NULL;
     wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     wcl.hIconSm = NULL;
     wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
     wcl.lpszMenuName = NULL;
     wcl.cbClsExtra = 0;
     wcl.cbWndExtra = 0;
     wcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

     if(!RegisterClassEx(&wcl)) {
          return FALSE;
     }

     g_hWnd = CreateWindowEx( 
          0,      
          g_lpszClassName,
          TEXT("MPEG3 Play Test"),
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT,      CW_USEDEFAULT,
          350, 150,
          NULL, 
          NULL, 
          hInstance, 
          NULL);

     if(!g_hWnd) {
          return FALSE;
     }

     ShowWindow( g_hWnd, nShowCmd);
     UpdateWindow(g_hWnd);

     MSG Msg;
     
     while(GetMessage(&Msg, NULL, 0, 0)) {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
     }

     return Msg.wParam;
     
}


//////////////////////////////////////////////////////////////////////////


LRESULT CALLBACK WndProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) {

     switch(Msg) {
     HANDLE_MSG (hwnd, WM_COMMAND, OnCommand );
     HANDLE_MSG(hwnd, WM_CREATE, OnCreate);
     HANDLE_MSG (hwnd, WM_DESTROY, OnDestroy );
     HANDLE_MSG(hwnd, WM_SIZE, OnSize);
     default:
          return DefWindowProc(hwnd, Msg, wParam, lParam);
     }

     return FALSE;
}


///////////////////////////////////////////////////////////////////////////////


unsigned int __stdcall MP3WorkerThread (PVOID pv) {

     LPTSTR pszFileName = (LPTSTR) pv;

     if ( !pszFileName ) {
          return 0;
     }

     HRESULT hr;

     hr = ::CoInitialize(NULL);

     assert ( SUCCEEDED(hr));
     
          IGraphBuilder* pGraphBuilder=NULL;
          IMediaControl* pMediaControl=NULL;
          IMediaEvent* pMediaEvent=NULL;

     hr = ::CoCreateInstance(
          CLSID_FilterGraph, 
          NULL, 
          CLSCTX_INPROC, 
          IID_IGraphBuilder, 
          (void**)&pGraphBuilder);
     assert ( SUCCEEDED(hr));
     
     hr = pGraphBuilder->QueryInterface(IID_IMediaControl, (void**)&pMediaControl);
     assert ( SUCCEEDED(hr) );

     hr = pGraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&pMediaEvent);
     assert ( SUCCEEDED (hr) );
     
     hr = pGraphBuilder->RenderFile( pszFileName, NULL);
     assert ( SUCCEEDED (hr));

     hr = pMediaControl->Run();
     assert ( SUCCEEDED (hr) );

     long code=0;
     hr = pMediaEvent->WaitForCompletion(INFINITE,&code);

     if( pMediaEvent ) 
          pMediaEvent->Release();
     if( pMediaControl )
          pMediaControl->Release();
     if( pGraphBuilder ) 
          pGraphBuilder->Release();

     ::CoUninitialize();

     free ( pszFileName );
     
     return 0;
}


///////////////////////////////////////////////////////////////////////////////


VOID PlayMP3 ( LPTSTR pszFileName ) {

     assert ( pszFileName );
     
     // allocate the memory for the file name buffer.
     LPTSTR pszBuff = NULL;
     INT nAllocSize = (_tcslen ( pszFileName ) + 1 ) * sizeof (TCHAR);
     pszBuff = (LPTSTR) malloc ( nAllocSize );
     assert (pszBuff);
     CopyMemory ( pszBuff, pszFileName, nAllocSize );

     // create a thread
     DWORD dwThread;
     HANDLE hThread = 
          (HANDLE)_beginthreadex(
               NULL, 
               NULL, 
               MP3WorkerThread, 
               (void*) pszBuff, 
               NULL, 
               (unsigned int*)&dwThread);

     assert ( hThread );
     
}


///////////////////////////////////////////////////////////////////////////////


void OnCommand(HWND hwnd, int nID, HWND hWndCtl, UINT codeNotify) {

     if ( IDM_BUTTON1 != nID ) {
          // Do nothing
          return;
     }

     TCHAR szFileName [MAX_PATH];
     ZeroMemory ( szFileName, sizeof ( szFileName ) );
     Edit_GetText ( g_hEdit, szFileName, sizeof ( szFileName ));

     PlayMP3 ( szFileName );
          
}


///////////////////////////////////////////////////////////////////////////////


BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) {

     // Button
     
     g_hButton = CreateWindowEx (
          0,
          TEXT("Button"),
          (LPTSTR) TEXT("OK"),
          WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
          hwnd, (HMENU) IDM_BUTTON1,
          NULL, NULL );

     // Edit
     
     g_hEdit = CreateWindowEx(
          WS_EX_CLIENTEDGE, 
          TEXT("Edit"),
          (LPTSTR) TEXT("C:\\foo.mp3"), 
          WS_CHILD | WS_VISIBLE,
          0, 0, 0,0, 
          hwnd, (HMENU) IDM_EDIT1,
          NULL, NULL); 

     // Show Windows
     
     ShowWindow ( g_hButton, SW_SHOW );
     ShowWindow ( g_hEdit, SW_SHOW );

     return TRUE;
}


///////////////////////////////////////////////////////////////////////////////


void OnDestroy ( HWND hwnd ) {

     PostQuitMessage(0);

}

///////////////////////////////////////////////////////////////////////////////


void OnSize(HWND hwnd, UINT state, int cx, int cy) {

     MoveWindow (
          g_hEdit,
          10, 
          10,
          300,
          20,
          TRUE);

     MoveWindow ( 
          g_hButton, 
          10, // left 
          35, // top, 
          75, // width
          20, // height
          TRUE );

}
スポンサーリンク