CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    2

    Borland C++ Builder, Play Sound

    Hello there,

    I am writing a code for a game. simply, I need to play a "wav" sound whenever the player click an object on the screen. I am using Borland C++ Builder 5.

    I have tried this by including:
    #include <MPlayer.hpp>

    TMediaPlayer *Mp;

    in my header file and

    Mp->filename = "sound27.wav";
    Mp->open();
    Mp->play();

    in my main code;

    I can compile and build the project. but whenever i play the game, when it reaches to the point that it should play the sound, it hangs !!

    appreciate your help. what should i do to solve this problem..

    thanks alot : )

  2. #2
    Join Date
    Jan 2007
    Posts
    38

    Re: Borland C++ Builder, Play Sound

    I've never used Borland's compiler or the TMediaPlayer class, but it looks like you're trying to use a class without instantiating an object of that class.

    At the moment, you have a pointer to a TMediaPlayer object, not the object itself. What's worse, since you haven't initialized the pointer, it points to random data. That means that, when you do Mp->filename = "sound27.wav"; it writes that string to a random location in memory, thereby causing the crash/hang up.

    Moral of the story: Always initialize your pointers! In this case, if you had done Mp = 0; before doing Mp->filename = "sound27.wav"; it would have thrown an access violation exception, which would've at least given you an idea where to look for the mistake.

    Now, how to solve the mistake? You can do one of 2 things:
    1. Create the object dynamically.
      Remember that I said that your pointer isn't initialized? The Mp variable was meant to point to an object of type TMediaPlayer, but you never created that object! However, you also didn't set the pointer to 0, which means its current value is the value that happened to be in memory when the bytes used to store the pointer were allocated, causing your crash as explained above. So you need to create a TMediaPlayer object, and have Mp point to it, like so: Mp = new TMediaPlayer;. When you're done with the object, destroy it by using: delete Mp;. The TMediaPlayer object is now destroyed, but the pointer still points to where it used to be (which is now random data again)! So set Mp to 0 to prevent the same kind of crashes from happening again.
    2. Create the object statically on the stack.
      This is probably the best do to for you, since it seems you're relatively new to C++ programming. Whay you do is change the TMediaPlayer *Mp; line in your header to TMediaPlayer Mp; (without the *). Mp is now the actual object itself, not a pointer to it. Use the object by writing:

      Mp.filename = "sound27.wav";
      Mp.open();
      Mp.play();

      Notice I replaced the ->'s with .'s!!


    I strongly advise you to read through some C++ texts again... The mistake you made is really a beginners mistake (although it happens to everyone who doesn't pay close attention). Even I could spot and explain it, and I don't have the language under my belt completely, either.
    I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here...

  3. #3
    Join Date
    Oct 2005
    Posts
    199

    Re: Borland C++ Builder, Play Sound

    Haven't used the TMediaPlayer -component, but the description of method says:

    C++
    __fastcall Open();

    Use Open to open a multimedia device. The multimedia device type must be specified in the DeviceType property before a device can be opened.
    'You help me, and I, in turn, am helped by you!'
    -H.S.

  4. #4
    Join Date
    Sep 2008
    Posts
    2

    Re: Borland C++ Builder, Play Sound

    Dear Jejoha and eero

    I am a beginner in C++, and I dont have any experience in it, I need to use it for a project. Thanks alot for your replies.

    I tried what you proposed but it gives me error.

    I tried to create the object dynamically as you proposed, it gives me the error as follow when compiling:
    "could not find a match for TMediaPlayer::TMediaPlayer()'.

    then I replaced "Mp = new TMediaPlayer;" with "Mp = new TMediaPlayer(this);" then I run it agian and this time it worked but when I am playing the game, when it reaches to where that it should play the sound, the program stops and gives the below error:

    "Project raised exception class EWin32Error with message "Win32 Error. Code:1400 invalid window handle'. Process stopped. Use step or Run to continue."

    this error appears on the line:
    Mp->Play();

    ...

    Then I tried to create the object staticlly,it gives me following errors while compiling:

    - [C++ Error] ExperimentThread.h(23): E2459 VCL style classes must be constructed using operator new
    ==> on "TMediaPlayer Mp;"


    - [C++ Error] ExperimentThread.cpp(31): E2279 Cannot find default constructor to initialize member 'ExperimentThread::Mp'
    ==>at brgining of my main code

    I also included:

    Mp->DeviceType = dtWaveAudio;

    as a device to play the wav. before play

    thanks for you help .. appreciate it !

  5. #5
    Join Date
    Oct 2005
    Posts
    199

    Re: Borland C++ Builder, Play Sound

    Try with:

    Code:
    TMediaPlayer* Mp = new TMediaPlayer(this->Handle);
    'You help me, and I, in turn, am helped by you!'
    -H.S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured