要不我直接给你们代码,你们帮我改下,让它可以实现音乐循环....
#include <windows.h> #include <stdio.h> #include <dshow.h> #include "resource.h"
#pragma comment(lib, "dxguid.lib") #pragma comment(lib, "strmiids.lib")
#pragma warning(disable : 4996)
#define Safe_Release(p) if((p)) (p)->Release();
// window handles, class. HWND g_hwnd; char g_class_name[] = "MP3PlayClass";
// DirectShows components IGraphBuilder* g_graph_builder = NULL; IMediaControl* g_media_control = NULL; IMediaEvent* g_media_event = NULL;
//-------------------------------------------------------------------------------- // Play mp3 which specified by filename. //-------------------------------------------------------------------------------- BOOL Play_MP3(char* filename) { // convert filename to wide-character string WCHAR w_filename[MAX_PATH] = {0};
mbstowcs(w_filename, filename, MAX_PATH);
// render the file g_graph_builder->RenderFile(w_filename, NULL);
// play the file, switches the entire filter graph into a running state. g_media_control->Run();
return TRUE; }
//-------------------------------------------------------------------------------- // Window procedure. //-------------------------------------------------------------------------------- long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; }
return (long) DefWindowProc(hwnd, msg, wParam, lParam); }
//-------------------------------------------------------------------------------- // Main function, routine entry. //-------------------------------------------------------------------------------- int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show) { WNDCLASS win_class; MSG msg;
// create window class and register it win_class.style = CS_HREDRAW | CS_VREDRAW; win_class.lpfnWndProc = Window_Proc; win_class.cbClsExtra = 0; win_class.cbWndExtra = DLGWINDOWEXTRA; win_class.hInstance = inst; win_class.hIcon = LoadIcon(inst, IDI_APPLICATION); win_class.hCursor = LoadCursor(NULL, IDC_ARROW); win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); win_class.lpszMenuName = NULL; win_class.lpszClassName = g_class_name;
if(! RegisterClass(&win_class)) return FALSE;
// create the main window g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_MP3PLAY), 0, NULL);
ShowWindow(g_hwnd, cmd_show); UpdateWindow(g_hwnd);
// initialize COM // // initialize the COM library on the current thread and identifies the concurrency model as single-thread // apartment (STA). CoInitialize(0);
// create the DirectMusic performance object // // creates a single uninitialized object of the class associated with a specified CLSID. if(FAILED(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&g_graph_builder))) { MessageBox(NULL, "Unable to create DirectShow Graph Builder object.", "Error", MB_OK); return FALSE; }
// Query for the media control and event objects g_graph_builder->QueryInterface(IID_IMediaControl, (void**)&g_media_control); g_graph_builder->QueryInterface(IID_IMediaEvent, (void**)&g_media_event); // play mp3 Play_MP3("escape.mp3"); // start message pump, waiting for signal to quit. ZeroMemory(&msg, sizeof(MSG));
while(msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); }
// get th status of the song, it if is done, exit program.
long event_code, param1, param2;
// retrieves the next notification event if(SUCCEEDED(g_media_event->GetEvent(&event_code, ¶m1, ¶m2, 1))) { if(event_code == EC_COMPLETE) { // frees resources associated with the parameters of an events. g_media_event->FreeEventParams(event_code, param1, param2);
break; } }
// frees resources associated with the parameters of an events. g_media_event->FreeEventParams(event_code, param1, param2); }
// stop music and relaese DirectShow objects
// switches all filters in the filter graph to a stopped state. g_media_control->Stop();
g_media_event->Release(); g_media_event = NULL;
g_media_control->Release(); g_media_control = NULL;
g_graph_builder->Release(); g_graph_builder = NULL;
UnregisterClass(g_class_name, inst);
// release COM system // // Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other // resources that the thread maintains, and forces all RPC connections on the thread to close. CoUninitialize(); return (int) msg.wParam; }
|