📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: DShow_PlayMP3
//
// Description: Example code showing how to play a MP3 file
//
// File Function: Main body of the program (main.cpp)
//
// Code:
// Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// dxguid.lib winmm.lib Strmiids.lib
//
// Local Files Required:
// main.cpp
// main.h
// DXUtil.cpp
//
// DX Files:
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"
//--------------------------------------------------------------------------------------
//
// Main windows function, ie main()
//
//--------------------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASSEX wndclass;
bool bRet;
// Set up window class
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = fnMessageProcessor;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Direct Show Demo"; // Registered Class Name
wndclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
// Register the window class
if( RegisterClassEx( &wndclass ) == NULL )
{
// The program failed, exit
exit(1);
}
// Create the window
hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW,
"Direct Show Demo", // Class Name
"DShow_PlayMP3", // Name Displayed on Title Bar
WS_OVERLAPPEDWINDOW,
0,
0,
320,
200,
NULL,
NULL,
hInstance,
NULL );
// Display the window
ShowWindow( hWnd, iCmdShow );
// Clear out message structure
ZeroMemory( &msg, sizeof(msg) );
// Play the title music
bRet = bPlayTitleMusic();
if( bRet == 0 ) {
MessageBox( hWnd, "Initialization Failure",
"Failed to initialize DirectShow",
MB_ICONEXCLAMATION|MB_OK );
// The program failed, exit
exit(1);
}
// Enter the message loop
while( msg.message!=WM_QUIT ) {
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else {
if( g_bBackgroundMusicActive ) {
vCheckMusicStatus();
}
}
}
// Clean up everything and exit the app
vCleanup();
UnregisterClass( "Direct Show Demo", wndclass.hInstance );
return 0;
}
//--------------------------------------------------------------------------------------
//
// Windows message processor
//
//--------------------------------------------------------------------------------------
LRESULT WINAPI fnMessageProcessor( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
default:
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
vStopTitleMusic();
}
//--------------------------------------------------------------------------------------
//
// Function to initialize the the sound graph and load
// the mp3 file
//
//--------------------------------------------------------------------------------------
bool bPlayTitleMusic( void )
{
HRESULT hr;
// Initialize COM
CoInitialize( NULL );
// Create the graph
CoCreateInstance( CLSID_FilterGraph,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void **)&g_pGraph );
// Query for interface objects
g_pGraph->QueryInterface( IID_IMediaControl, (void **)&g_pMediaControl );
g_pGraph->QueryInterface( IID_IMediaEvent, (void **)&g_pEvent );
g_pGraph->QueryInterface( IID_IMediaSeeking, (void **)&g_pSeeking );
// Load the song (insert your own file name below)
hr = g_pGraph->RenderFile(L"c:\\dxsdk\\samples\\media\\track3.mp3", NULL);
if( hr != S_OK ) {
return( 0 );
}
// Set the playback rate
g_pSeeking->SetRate( 1.25 );
// Play the song
g_pMediaControl->Run();
// Set background music active flag
g_bBackgroundMusicActive = 1;
return( 1 );
}
//--------------------------------------------------------------------------------------
//
// Function to clean up the graph and allocated memory
//
//--------------------------------------------------------------------------------------
void vStopTitleMusic( void )
{
// Stop the song if playing
g_pMediaControl->Stop();
// Clean up
g_pMediaControl->Release();
g_pEvent->Release();
g_pGraph->Release();
}
//--------------------------------------------------------------------------------------
//
// Function to check the status of the music to see if it is done and
// needs to be restarted
//
//--------------------------------------------------------------------------------------
void vCheckMusicStatus( void )
{
long evCode;
// Check the event code
g_pEvent->WaitForCompletion( 0, &evCode );
// If the music is done, restart it
if( evCode == EC_COMPLETE ) {
// Set the starting position to 0
LONGLONG lStartPos = 0;
// Stop the music
g_pMediaControl->Stop();
// Set the positions
g_pSeeking->SetPositions( &lStartPos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning );
// Run the music
g_pMediaControl->Run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -