none
Easy way to play a mp3 file in memory? RRS feed

  • Question

  • I need to play asynchronously mp3 files which have been loaded in the memory. In other words, I need a mp3 version of PlaySound() with the flags 'SND_MEMORY' and 'SND_ASYNC'. The mp3 files are very short simple files distributed along with our program. DirectShow seems to meet our needs, but it looks quite complicated after I checked out its sample projects, also a little overkill. Does anyone has any easier ways to do what I need? I think it's a pretty simple and common task. Thanks!
    Wednesday, February 4, 2009 1:32 AM

Answers

  • worst case, save the file to the local settings folder of the target machine?

     

    Actually looking at FMOD ex api there is this which you can use with the FMOD_OPENMEMORRY:

     

    FMOD_RESULT System::createSound( const char * name_or_data, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO * exinfo, FMOD::Sound ** sound );

    System::createSound

    System::createSound

    Parameters

    name_or_data

    Name of the file or URL to open, or pointer to memory block if FMOD_OPENMEMORY/FMOD_OPENMEMORY_POINT is used. For CD playback the name should be a drive letter with a colon, example "D:" (windows only).

    mode

    Behaviour modifier for opening the sound. See FMOD_MODE. Also see remarks for more.

    exinfo

    Pointer to a FMOD_CREATESOUNDEXINFO which lets the user provide extended information while playing the sound. Optional. Specify 0 or NULL to ignore.

    sound

    Address of a variable to receive a newly created FMOD::Sound object.

     

     Probably best from what I gather your requirements/usage are to wrap it in a simpler class.

    • Marked as answer by Lastsmoke Thursday, February 5, 2009 1:47 PM
    Wednesday, February 4, 2009 6:53 PM

All replies

  • Perhaps this thread that lists a few libraries you can use may be of help?

    http://stackoverflow.com/questions/428884/how-to-play-mp3-files-in-c

    eg:
    http://www.mpg123.org/

    FMod looks like a runner:
    http://www.fmod.org/index.php/download

    Using FMOD (cross platform), this should be as simple as this:

    #include <conio.h>
    #include "inc/fmod.h"

    FSOUND_SAMPLE
    * handle;

    int main ()
    {
       
    // init FMOD sound system
       FSOUND_Init
    (44100, 32, 0);

       
    // load and play mp3
       handle
    =FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
       FSOUND_PlaySound
    (0,handle);

       
    // wait until the users hits a key to end the app
       
    while (!_kbhit())
       
    {
       
    }

       
    // clean up
       FSOUND_Sample_Free
    (handle);
       FSOUND_Close
    ();
    }




    Thanks.
    Wednesday, February 4, 2009 9:12 AM
  • Many thanks for your reply, Mark! At a glance, the libs don't do what I want, the MP3 files I need to play back are already in the memory, I don't have the file handle. I will look into them to see if they can do the work...will let you know. Thanks again.
    Wednesday, February 4, 2009 6:13 PM
  • worst case, save the file to the local settings folder of the target machine?

     

    Actually looking at FMOD ex api there is this which you can use with the FMOD_OPENMEMORRY:

     

    FMOD_RESULT System::createSound( const char * name_or_data, FMOD_MODE mode, FMOD_CREATESOUNDEXINFO * exinfo, FMOD::Sound ** sound );

    System::createSound

    System::createSound

    Parameters

    name_or_data

    Name of the file or URL to open, or pointer to memory block if FMOD_OPENMEMORY/FMOD_OPENMEMORY_POINT is used. For CD playback the name should be a drive letter with a colon, example "D:" (windows only).

    mode

    Behaviour modifier for opening the sound. See FMOD_MODE. Also see remarks for more.

    exinfo

    Pointer to a FMOD_CREATESOUNDEXINFO which lets the user provide extended information while playing the sound. Optional. Specify 0 or NULL to ignore.

    sound

    Address of a variable to receive a newly created FMOD::Sound object.

     

     Probably best from what I gather your requirements/usage are to wrap it in a simpler class.

    • Marked as answer by Lastsmoke Thursday, February 5, 2009 1:47 PM
    Wednesday, February 4, 2009 6:53 PM
  • Thanks, Mark! I decided to use DirectShow. MS lib makes me feel safe.
    Thursday, February 5, 2009 1:49 PM
  • Glad to be of help.

    Actually have you thought about using the windows media player library?

    http://msdn.microsoft.com/en-us/windowsmedia/bb190309.aspx (Windows Media Player 10 SDK)

    You could create an instance of it and play most file formats, of course you'd have to hide the window :

    http://msdn.microsoft.com/en-us/library/bb249370(VS.85).aspx

    If you want a simple wrapper for the media player 10 sdk, let me know and I'll drop you a private email.

    Thanks.
    Thursday, February 5, 2009 2:04 PM
  • Hi,

    I have coded a small MP3Player C++ class in a single .h that is able to play a mp3 from a memory location using plain windows API .It doesn't rely on any 3rd party plugins like Bass or FMod.

    Through, this implementation doesn't perform any async playing, It is really easy to add it. This is probably a good starting point while you will be able to modify it to fit your needs.


    Alexandre Mutel - Code4k
    Saturday, May 22, 2010 8:45 AM
  • The Windows Media libraries have been removed from the linked page on MSDN, Media Technologies for Windows now is the page that has all the downloads and links for developer content related to media playback, encoding, transcoding, and protection.

    -Gus

    Tuesday, December 14, 2010 10:34 PM