Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem

PlaySound(MAKEINTRESOURCE, IDR_WAVE_RANDOM... problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionloungelearning
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lctrncs
    wrote on last edited by
    #1

    The need to compile wav files into a Doc/View plus modeless dialog MFC program led to abandoning a functioning mechanism to play random wav files:

    PlaySound(“m_sStrConcatenatedStringWithPath”,NULL,SND_FILENAME|SND_SYNC);

    (where sStrConcatenatedStringWithPath = “/path/201.wav” etc.) and adopting:

    LPTSTR pRandomSound = m_sStrConcatenatedStringResource.GetBuffer(0);

    PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

    m_sStrConcatenatedStringResource.ReleaseBuffer(0);

    (where m_sStrConcatenatedStringResource = “IDR_WAVE_201” etc.). ...that fails to play the wav files even when...

    PlaySound(MAKEINTRESOURCE, IDR_WAVE_201, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

    will play the IDR_WAVE_201 resource. Many attempts experimenting different ways to cast the CString to the LPTSTR and obtaining the handle to the executable led to the code above, which still will not play the random sound resource. Suggestions?

    "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

    D I L 3 Replies Last reply
    0
    • L lctrncs

      The need to compile wav files into a Doc/View plus modeless dialog MFC program led to abandoning a functioning mechanism to play random wav files:

      PlaySound(“m_sStrConcatenatedStringWithPath”,NULL,SND_FILENAME|SND_SYNC);

      (where sStrConcatenatedStringWithPath = “/path/201.wav” etc.) and adopting:

      LPTSTR pRandomSound = m_sStrConcatenatedStringResource.GetBuffer(0);

      PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

      m_sStrConcatenatedStringResource.ReleaseBuffer(0);

      (where m_sStrConcatenatedStringResource = “IDR_WAVE_201” etc.). ...that fails to play the wav files even when...

      PlaySound(MAKEINTRESOURCE, IDR_WAVE_201, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

      will play the IDR_WAVE_201 resource. Many attempts experimenting different ways to cast the CString to the LPTSTR and obtaining the handle to the executable led to the code above, which still will not play the random sound resource. Suggestions?

      "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      lctrncs wrote:

      PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

      What PlaySound() are you using that takes four arguments?

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      L 1 Reply Last reply
      0
      • D David Crow

        lctrncs wrote:

        PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

        What PlaySound() are you using that takes four arguments?

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        L Offline
        L Offline
        lctrncs
        wrote on last edited by
        #3

        Thank you for responding. BOOL PlaySound( LPCSTR pszSound, HMODULE fdwSound, DWORD fdwSound) There are two arguments: LPCSTR (pszSound) and HMODULE (fdwSound) pRandomSound and GetModuleHandle(NULL) and two flags: DWORD fdwSound SND_SYNC|SND_RESOURCE Thanks again. I hope this answers your question.

        "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

        D 1 Reply Last reply
        0
        • L lctrncs

          Thank you for responding. BOOL PlaySound( LPCSTR pszSound, HMODULE fdwSound, DWORD fdwSound) There are two arguments: LPCSTR (pszSound) and HMODULE (fdwSound) pRandomSound and GetModuleHandle(NULL) and two flags: DWORD fdwSound SND_SYNC|SND_RESOURCE Thanks again. I hope this answers your question.

          "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          lctrncs wrote:

          BOOL PlaySound( LPCSTR pszSound, HMODULE fdwSound, DWORD fdwSound) There are two arguments: LPCSTR (pszSound) and HMODULE (fdwSound) pRandomSound and GetModuleHandle(NULL) and two flags: DWORD fdwSound SND_SYNC|SND_RESOURCE

          You seem to be confused. Look at the code snippet in your original post. You are passing PlaySound() four arguments: 1) MAKEINTRESOURCE, 2) pRandomSound, 3) GetModuleHandle(NULL), and 4) SND_SYNC|SND_RESOURCE. So my question still remains: where does this four-argument function come from? The answer to this may end up having no bearing on the actual problem, but if you can't post a working code snippet, it's kind of hard to come up with a useful answer.

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          L 1 Reply Last reply
          0
          • D David Crow

            lctrncs wrote:

            BOOL PlaySound( LPCSTR pszSound, HMODULE fdwSound, DWORD fdwSound) There are two arguments: LPCSTR (pszSound) and HMODULE (fdwSound) pRandomSound and GetModuleHandle(NULL) and two flags: DWORD fdwSound SND_SYNC|SND_RESOURCE

            You seem to be confused. Look at the code snippet in your original post. You are passing PlaySound() four arguments: 1) MAKEINTRESOURCE, 2) pRandomSound, 3) GetModuleHandle(NULL), and 4) SND_SYNC|SND_RESOURCE. So my question still remains: where does this four-argument function come from? The answer to this may end up having no bearing on the actual problem, but if you can't post a working code snippet, it's kind of hard to come up with a useful answer.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            L Offline
            L Offline
            lctrncs
            wrote on last edited by
            #5

            The BOOL PlaySound( LPCSTR pszSound, HMODULE fdwSound, DWORD fdwSound) comes from the "Multimedia: Platform SDK" section of the Visual C++ 6.0 MSDEV documentation. This specific use of the MAKEINTRESOURCE macro is documented in the "Technical Articles" section of the Visual C++ 6.0 MSDEV documentation in a file called "Moving Your Game to Windows, Part III. Does this answer you question?

            "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

            1 Reply Last reply
            0
            • L lctrncs

              The need to compile wav files into a Doc/View plus modeless dialog MFC program led to abandoning a functioning mechanism to play random wav files:

              PlaySound(“m_sStrConcatenatedStringWithPath”,NULL,SND_FILENAME|SND_SYNC);

              (where sStrConcatenatedStringWithPath = “/path/201.wav” etc.) and adopting:

              LPTSTR pRandomSound = m_sStrConcatenatedStringResource.GetBuffer(0);

              PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

              m_sStrConcatenatedStringResource.ReleaseBuffer(0);

              (where m_sStrConcatenatedStringResource = “IDR_WAVE_201” etc.). ...that fails to play the wav files even when...

              PlaySound(MAKEINTRESOURCE, IDR_WAVE_201, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

              will play the IDR_WAVE_201 resource. Many attempts experimenting different ways to cast the CString to the LPTSTR and obtaining the handle to the executable led to the code above, which still will not play the random sound resource. Suggestions?

              "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

              I Offline
              I Offline
              iraclyKv
              wrote on last edited by
              #6

              When You add WAV resource by ID there is no way to call PlaySound (and other resource functions) by resource name and vice versa - when you add resource with resource name string - You can not access resource by it' ID. For this resource script file:

              //Resources.h
              #define SOUND_1

              //MyApp.rc
              ...
              SOUND_1 WAV "snd1.wav"
              "MOO" WAV "moo.wav"

              Valid PlaySound call is:

              PlaySound(MAKEINTRESOURCE(SOUND_1), ...);
              PlaySound("MOO", ...);

              1 Reply Last reply
              0
              • L lctrncs

                The need to compile wav files into a Doc/View plus modeless dialog MFC program led to abandoning a functioning mechanism to play random wav files:

                PlaySound(“m_sStrConcatenatedStringWithPath”,NULL,SND_FILENAME|SND_SYNC);

                (where sStrConcatenatedStringWithPath = “/path/201.wav” etc.) and adopting:

                LPTSTR pRandomSound = m_sStrConcatenatedStringResource.GetBuffer(0);

                PlaySound(MAKEINTRESOURCE, pRandomSound, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

                m_sStrConcatenatedStringResource.ReleaseBuffer(0);

                (where m_sStrConcatenatedStringResource = “IDR_WAVE_201” etc.). ...that fails to play the wav files even when...

                PlaySound(MAKEINTRESOURCE, IDR_WAVE_201, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE);

                will play the IDR_WAVE_201 resource. Many attempts experimenting different ways to cast the CString to the LPTSTR and obtaining the handle to the executable led to the code above, which still will not play the random sound resource. Suggestions?

                "For a successful technology, reality must take precedence over public relations, for nature cannot be fooled." Richard Feynman, Minority Report to the Official Report on the Space Shuttle Challenger Crash

                L Offline
                L Offline
                lctrncs
                wrote on last edited by
                #7

                Thanks to everyone at CodeProject! It appears that the original post may not be clear. Therefore, consider the following code snippet which includes much more information about the problem and the (failed) attemtps to resolve it. Again, for reasons this beginner has yet to understand, PlaySound works fine with a filename constructed from a CString and a random number (/path/filename1.wav), but fails when concatenated CStrings are used to create a resouce identifier (IDR_WAVE_1). Please consider the code and coments below.

                //Create random resource ID (similar approach used to create random wave file names)

                int m\_nRandomSound;
                m\_nRandomSound = getrandom( 500, 509  );		//Place the random number into the variable n\_nRandomRecord
                
                CString m\_sStrRandomSound;						
                m\_sStrRandomSound = "";
                
                CString m\_sStrRandomResourcePrefix;			//Create the resource prefix
                m\_sStrRandomResourcePrefix = "IDR\_WAVE\_";
                
                m\_sStrRandomSound.Format("%d", m\_nRandomSound);	//Convert the random integer to a CString
                

                //Concatentate the strings into the resource identifier
                CString m_sStrRandomResourcePrefixWav;
                m_sStrRandomResourcePrefixWav = m_sStrRandomResourcePrefix + m_sStrRandomSound;

                //Set the resource identifier directly
                CString m_sStrRandomResourcePrefixWav2;
                m_sStrRandomResourcePrefixWav2="IDR_WAVE_503";

                //
                //
                //Demonstrate function similar method that plays sounds using concatenated CString of path/filename
                PlaySound(m_sStrRandomSoundPathWav, GetModuleHandle(NULL), SND_FILENAME|SND_SYNC );
                //PLAYS from concatenated CString
                //(e.g. \path\filename?.wav - where ? is an integer converted to a CString)

                PlaySound(MAKEINTRESOURCE(IDR_WAVE_503), GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE );
                //PLAYS IDR_WAVE_503
                //which is directly entered an not subtituted in any way
                //
                //
                //ATTEMPTS TO EMPLOY CONCATENATED STRING TO PLAY RANDOM WAVE FILE

                //PlaySound(MAKEINTRESOURCE(m_sStrRandomResourcePrefixWav), GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE );
                //error C2440: 'type cast' : cannot convert from 'class CString' to 'unsigned short'
                //included for completeness

                //PlaySound(IDR_WAVE_503, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE );
                //error C2664: 'PlaySoundA' : cannot convert parameter 1 from 'const int' to 'const char *'
                //included for completeness

                PlaySound(m_sStrRandomResourcePrefixWav2, GetModuleHandle(NULL), SND_SYNC|SND_RESOURCE );
                //Compiles b

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups