Thread: How do I play an MP3?

  1. #16
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Why the hell dont you just include the winmm library????
    It will work fine then.

  2. #17
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    >>How do I play an MP3?

    I use MusicMatch Jukebox.




    j/k... have you tried FMOD? It's a free API for sound and music (free for non-commercial use at least, and then the license aren't very expensive for small shareware dev).

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    thanks, I was sort of wondering where it came from
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    oh... uhh... by the way, im using Microsoft Visual C++ 6 Pro edition... and to tell you the truth, i have no idea where to put the -|winmm thingee in. I tried in a few places, but it just generated more errors. And also, about the mciSendCommand thingee, my compiler said "unresolved external symbol __imp__mciSendCommandA@16". Is that linked to winmm too?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    ... ok, so never mind about the last post. I think that I've got the library in, somehow, but now the thing won't play mp3's. PlaySound plays wavs, but not mp3's, and the mciSendCommand doesn't play mp3's but I havent tried it with wavs. I thought mciSendCommand should play mp3's though?...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    > have you tried FMOD?
    HOW DARE U STEAL MY SUGGESTIONS!
    hehe
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  7. #22
    Davros
    Guest
    > I thought mciSendCommand should play mp3's though?...

    I can assure you that it does. Here's my full code. Header followed by cpp. I use Borland C++ Builder, hence the AnsiString type (an equivilent to STL string). It should be trivial to port this code to VC++.

    Sorry for the long post, but hope this is helpful.



    Code:
    #ifndef audio_playerH
    #define audio_playerH
    
    #include <mmsystem.h>
    //---------------------------------------------------------------------------
    // CLASS AudioPlayer
    //---------------------------------------------------------------------------
    class AudioPlayer
    {
    private:
      bool pPaused;
      bool pErr;
      char pErrStr[128];
      MCIDEVICEID pDevice;
      AnsiString pFileName;
      bool open(const AnsiString& fName, int deviceType, bool playNow);
      int setErrCode(int errCode);
      void setErrStr(char* estr);
    public:
      AudioPlayer();
      ~AudioPlayer();
      bool openWav(const AnsiString& fName, bool playNow = false);
      bool openMp3(const AnsiString& fName, bool playNow = false);
      bool openMidi(const AnsiString& fName, bool playNow = false);
      AnsiString getFileName() const;
      bool play();
      void stop();
      void close();
      void pause();
      int length();
      int position();
      bool isOpen();
      bool playing();
      bool paused();
      bool err() const;
      AnsiString errStr() const;
    };
    //---------------------------------------------------------------------------
    #endif

    Code:
    #include <vcl.h> // This is a Borland include
    #pragma hdrstop
    
    #include "audio_player.h"
    #include <sysutils.hpp>
    #include <string.h>
    //---------------------------------------------------------------------------
    // CLASS AudioPlayer : PRIVATE METHODS
    //---------------------------------------------------------------------------
    bool AudioPlayer::open(const AnsiString& fName, int deviceType, bool playNow)
    {
      // Open new file
      close();
    
      if (!pErr)
      {
        MCI_OPEN_PARMS op;
        op.dwCallback = NULL;
        op.lpstrDeviceType = (char*)deviceType;
        op.lpstrElementName = fName.c_str();
        op.lpstrAlias = NULL;
    
        int flags = 0;
        if (deviceType != MCI_ALL_DEVICE_ID)
          flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID;
    
        // Send command to
        if ( setErrCode( mciSendCommand( NULL, MCI_OPEN,
          MCI_OPEN_ELEMENT | MCI_WAIT | flags,
          (DWORD)&op) ) == 0)
        {
          // Success on open - set time format to milliseconds
          pDevice = op.wDeviceID;
          pFileName = fName;
    
          MCI_SET_PARMS sp;
          sp.dwCallback = NULL;
          sp.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
          if ( setErrCode( mciSendCommand(pDevice, MCI_SET,
            MCI_SET_TIME_FORMAT | MCI_WAIT, (DWORD)&sp) ) == 0)
          {
            if (playNow)
              return play();
            else
              return true;
          }
          else
            close();
        }
      }
    
      return false;
    }
    //---------------------------------------------------------------------------
    int AudioPlayer::setErrCode(int errCode)
    {
      // Set error code
      if (errCode == 0)
      {
        pErr = false;
        pErrStr[0] = '\0';
      }
      else
      {
        if (!mciGetErrorString(errCode, pErrStr, sizeof(pErrStr)))
          strcpy(pErrStr, "Unknown media device error");
    
        pErr = true;
      }
    
      return errCode;
    }
    //---------------------------------------------------------------------------
    void AudioPlayer::setErrStr(char* estr)
    {
      // Set error string
      if (estr == NULL)
      {
        pErr = false;
        pErrStr[0] = '\0';
      }
      else
      {
        strcpy(pErrStr, estr);
        pErr = true;
      }
    }
    //---------------------------------------------------------------------------
    // CLASS AudioPlayer : PUBLIC METHODS
    //---------------------------------------------------------------------------
    AudioPlayer::AudioPlayer()
    {
      // Constructor
      pPaused = false;
      setErrCode(0);
    }
    //---------------------------------------------------------------------------
    AudioPlayer::~AudioPlayer()
    {
      // Destructor - stop playing
      try
      {
        close();
      }
      catch(...)
      {
      }
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::openWav(const AnsiString& fName, bool playNow)
    {
      // Open wave audio
      return open(fName, MCI_DEVTYPE_WAVEFORM_AUDIO, playNow);
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::openMp3(const AnsiString& fName, bool playNow)
    {
      // open mp3
      return open(fName, MCI_ALL_DEVICE_ID, playNow);
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::openMidi(const AnsiString& fName, bool playNow)
    {
      // Open midi
      return open(fName, MCI_DEVTYPE_SEQUENCER, playNow);
    }
    //---------------------------------------------------------------------------
    AnsiString AudioPlayer::getFileName() const
    {
      // Return open filename
      return pFileName;
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::play()
    {
      // Play current audio file or resume playing
      if (pDevice != NULL)
      {
        if (pPaused)
        {
          // Play from current position
          MCI_PLAY_PARMS pp;
          pp.dwCallback = NULL;
    
          if ( setErrCode( mciSendCommand(pDevice, MCI_PLAY, MCI_NOTIFY, (DWORD)&pp) ) == 0)
          {
            pPaused = false;
            return true;
          }
        }
        else
        {
          // Play file from start
          stop();
    
          if (!pErr)
          {
            MCI_PLAY_PARMS pp;
            pp.dwCallback = NULL;
            pp.dwFrom = 0;
            if ( setErrCode( mciSendCommand(pDevice, MCI_PLAY, MCI_NOTIFY | MCI_FROM, (DWORD)&pp) ) == 0)
              return true;
          }
        }
      }
      else
        setErrStr("MCI audio device is closed");
    
      return false;
    }
    //---------------------------------------------------------------------------
    void AudioPlayer::stop()
    {
      // Stop playing (& rewind)
      pPaused = false;
      setErrCode(0);
    
      if (pDevice != NULL)
      {
        MCI_GENERIC_PARMS gp;
        gp.dwCallback = NULL;
    
        setErrCode( mciSendCommand(pDevice, MCI_STOP, MCI_WAIT, (DWORD)&gp) );
      }
    }
    //---------------------------------------------------------------------------
    void AudioPlayer::close()
    {
      // Stop & reset filename
      stop();
    
      if (pDevice != NULL)
      {
        MCI_GENERIC_PARMS gp;
        gp.dwCallback = NULL;
    
        if ( setErrCode( mciSendCommand(pDevice, MCI_CLOSE, MCI_WAIT, (DWORD)&gp) ) == 0)
        {
          pFileName = "";
          pDevice = NULL;
        }
      }
    }
    //---------------------------------------------------------------------------
    void AudioPlayer::pause()
    {
      // Pause playing (by calling stop)
      stop();
    
      if (pErr == 0)
        pPaused = true;
    }
    //---------------------------------------------------------------------------
    int AudioPlayer::length()
    {
      // Get audio length in millisecs
      setErrCode(0);
    
      if (pDevice != NULL)
      {
        MCI_STATUS_PARMS sp;
        sp.dwCallback = NULL;
        sp.dwItem = MCI_STATUS_LENGTH;
    
        if ( setErrCode( mciSendCommand(pDevice, MCI_STATUS,
          MCI_WAIT | MCI_STATUS_ITEM, (DWORD)&sp) ) == 0)
          return sp.dwReturn;
      }
    
      return -1;
    }
    //---------------------------------------------------------------------------
    int AudioPlayer::position()
    {
      // Get audio position in millisecs
      setErrCode(0);
    
      if (pDevice != NULL)
      {
        MCI_STATUS_PARMS sp;
        sp.dwCallback = NULL;
        sp.dwItem = MCI_STATUS_POSITION;
    
        if ( setErrCode( mciSendCommand(pDevice, MCI_STATUS,
          MCI_WAIT | MCI_STATUS_ITEM, (DWORD)&sp) ) == 0)
          return sp.dwReturn;
      }
    
      return -1;
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::isOpen()
    {
      // Is audio file open
      setErrCode(0);
      return (pDevice != NULL);
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::playing()
    {
      // Is audio playing
      setErrCode(0);
    
      if (pDevice != NULL)
      {
        MCI_STATUS_PARMS sp;
        sp.dwCallback = NULL;
        sp.dwItem = MCI_STATUS_MODE;
    
        if ( setErrCode(mciSendCommand(pDevice, MCI_STATUS,
          MCI_WAIT | MCI_STATUS_ITEM, (DWORD)&sp) ) == 0)
          return (sp.dwReturn == MCI_MODE_PLAY);
      }
    
      return false;
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::paused()
    {
      // Is audio paused
      setErrCode(0);
      return pPaused;
    }
    //---------------------------------------------------------------------------
    bool AudioPlayer::err() const
    {
      // Was an error raised in the last operation
      return pErr;
    }
    //---------------------------------------------------------------------------
    AnsiString AudioPlayer::errStr() const
    {
      // Get error string
      if (pErr)
        return pErrStr;
      else
        return "";
    }
    //---------------------------------------------------------------------------

  8. #23
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Rename them to .wav then. Works for me =P

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    thanks, i'll try it
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #25
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Okiesmokie
    > have you tried FMOD?
    HOW DARE U STEAL MY SUGGESTIONS!
    hehe
    Oops! I didn't even notice that.

  11. #26
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    ho humm... there's a problem with my mmsystem.h. I tried compiling and it found some errors:

    --------------------Configuration: Media Player - Win32 Debug--------------------
    Compiling...
    mp.cpp
    c:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : error C2146: syntax error : missing ';' before identifier 'MMVERSION'
    c:\program files\microsoft visual studio\vc98\include\mmsystem.h(113) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    Media Player.exe - 2 error(s), 0 warning(s)



    This was where the error occured (in mmsystem.h):
    Code:
    #ifdef _WIN32
    typedef UINT        MMVERSION;  /* major (high byte), minor (low byte) */
    Is my header corrupted, or did I miss something?...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #27
    you should include the windows.h header.

    Add #include <windows.h> to your program


    BTW, never change a header of msvc++ (or an other compiler)

  13. #28
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    To add the winmm library add the following line. You are using VC++ right?

    #pragma comment (lib , "winmm.lib")
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  14. #29
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Unhappy

    umm... ok, I'll try using the pragma thing, but the problem really was windows.h tho doh, it wasn't in the AudioPlayer header but it was in my main file.

    but... that's where it goes downhill. It still doesn't play the mp3!!! The only possible thing I can think of was that (and I doubt this is the prob) it couldn't find <sysutils.hpp>. Any better ideas?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. play MP3 on win32 consol project
    By Skull in forum C++ Programming
    Replies: 11
    Last Post: 02-09-2008, 04:30 PM
  2. not able to play MP3 files in SUSE 10.0
    By kris.c in forum Tech Board
    Replies: 0
    Last Post: 09-14-2006, 04:36 AM
  3. Mp3 play - MCI
    By geek@02 in forum Windows Programming
    Replies: 4
    Last Post: 09-30-2004, 08:07 PM
  4. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM
  5. Play Mp3...
    By Jperensky in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2003, 02:08 PM