MP3 Player

Suppose one wants to write a program that plays latest version of files in the widely used mp3 format. What libraries (or functions, methods, etc) should one include in their program in the first place. How can one make language instructions do something like that? There must be more clever ways of doing things than doing them all over again. Having the source code available of such programs available with adequate, almost step-by-step explanations, is always a great boon, though they may be costly. One may make alterations as they wish, but that is not the expectation here, although it is desirable. Getting the right codecs to play files of a desired format can either be done freely or associated with cost.
There does not seem to be much literature that 1) Provides an outline of what should be done to make a fully fledged program from scratch. 2) Covers extra guidance and/or information on any part of the outline one would like to expand on. There is certainly a large body of literature on language instructions or, roughly speaking, commands (eg getch(), putch(), etc). But useful as it may be when invoked at exactly the right time and used as appropriate reference, that is definitely not what is meant here. 3) Points to practical, tangible tools (eg libraries) one is going to use, at least basic ones. Such a piece of literature would tell one that if they are going to write (or make) a media player program that includes coverage of such and such media libraries that amount to a hypothetical 23 files.
Resolving such a barrier to either visual, console, Windows or API programming may introduce a boom to the programmer community, just like the introduction of visual programming did. It may lead to rise of new figures in the programming communities while it may extend junk programming as well. Experience shows the latter should not pose a risk to the software community.
I believe MP3 is a closed-format which means it'll be tough to find documentation on it. Fortunately there are some alternatives.

You could use libmp3lame. Not sure if it works for windows but check this out:
http://packages.ubuntu.com/quantal/lame-doc
Windows can play MP3 by default, no external libraries are needed, the decoder is built-in.

You can use mciSendString() API to play mp3 files on windows.
http://msdn.microsoft.com/en-us/library/ms709492%28VS.85%29.aspx

Example code can be found here:
http://www.daniweb.com/software-development/c/code/268167/play-sound-files-via-mcisendstring-commands


Or you can use DirectShow :)
I found some example (not tested):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Visual C++ example
#include <dshow.h>
#include <cstdio>
// For IID_IGraphBuilder, IID_IMediaControl, IID_IMediaEvent
#pragma comment(lib, "strmiids.lib") 

// Obviously change this to point to a valid mp3 file.
const wchar_t* filePath = L"C:/example.mp3"; 

int main()
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;

    // Initialize the COM library.
    HRESULT hr = ::CoInitialize(NULL);
    if (FAILED(hr))
    {
        ::printf("ERROR - Could not initialize COM library");
        return 0;
    }

    // Create the filter graph manager and query for interfaces.
    hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        ::printf("ERROR - Could not create the Filter Graph Manager.");
        return 0;
    }

    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph.
    hr = pGraph->RenderFile(filePath, NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    // Clean up in reverse order.
    pEvent->Release();
    pControl->Release();
    pGraph->Release();
    ::CoUninitialize();
}


However, I suggest you to use BASS:
http://www.un4seen.com/
Last edited on
Topic archived. No new replies allowed.