Hi

I am relatively new to c++ but have written a program that would benefit from a little background music. I need it to be mp3 format. I am using dev c++ on windows XP.

can anyone help?

Dani AI

Generated

A compact, practical add-on: on Win32 (Windows XP) the in-process DirectShow filter graph is a reliable way to play MP3s and implement looping without launching an external player. It gives event callbacks, seeks, and basic volume control so background music can run continuously while the app stays responsive. As and hinted, there are both OS-level and third-party routes; DirectShow is the OS-level route that fits neatly into a C++ program.

// Minimal DirectShow loop example.
// Requires <dshow.h> and linking with Strmiids.lib
#include <windows.h>
#include <dshow.h>

int main()
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr)) return -1;

    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                          IID_IGraphBuilder, (void**)&pGraph);
    if (SUCCEEDED(hr)) {
        hr = pGraph->RenderFile(L"C:\\path\\to\\bg.mp3", NULL);
        if (SUCCEEDED(hr)) {
            pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
            pGraph->QueryInterface(IID_IMediaEvent, (void**)&pEvent);
            pControl->Run();

            long evCode = 0;
            while (SUCCEEDED(pEvent->WaitForCompletion(INFINITE, &evCode))) {
                if (evCode == EC_COMPLETE) {
                    IMediaSeeking *pSeek = NULL;
                    if (SUCCEEDED(pGraph->QueryInterface(IID_IMediaSeeking, (void**)&pSeek))) {
                        LONGLONG pos = 0;
                        pSeek->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
                        pSeek->Release();
                        pControl->Run(); // restart
                    } else break;
                } else break;
            }
        }
    }

    if (pControl) pControl->Stop();
    if (pEvent) pEvent->Release();
    if (pControl) pControl->Release();
    if (pGraph) pGraph->Release();
    CoUninitialize();
    return 0;
}

Notes and troubleshooting:

  • Run the graph on a worker thread for GUI apps so playback does not block the UI. Handle cleanup on program exit (stop graph, release interfaces, CoUninitialize).
  • If RenderFile fails, check the file path (use wide strings as above) and that an MP3 decoder/codec is present on the machine; older XP installs may lack codecs. If Dev-C++ lacks DirectShow headers/libs, install the Windows SDK or consider a small third-party library that bundles decoders for easier deployment.
  • Volume can be controlled via the DirectShow IBasicAudio interface; seeking and EC_COMPLETE handling implemented above achieve seamless looping.

Recommended Answers

All 3 Replies

I've never used Dev-C++, but it should have a Windows MediaPlayer component that you can drop on your application, and use it to play the MP3.

You can also google "msdn media control interface" for the APIs you can use to manipulate media devices on Windows.

Good luck.

try using FMOD.

Will this play continuously? if not do you know how i can do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.