No sound playing

This is a discussion on No sound playing within the C# Programming forums, part of the General Programming Boards category; I am creating a game and using System.Media.SoundPlayer() to play a sound. It works nicely when running from Visual Studio. ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    No sound playing

    I am creating a game and using System.Media.SoundPlayer() to play a sound.
    It works nicely when running from Visual Studio. But If I double click the .exe the sound isn't heard. Like:

    Code:
                    mPlayer = new System.Media.SoundPlayer();
                    mPlayer.LoadAsync();
                    ...
                    mPlayer.SoundLocation = "Sounds\\Sword.wav";
                    mPlayer.Play();
                    ...
    No errors, which means it fines the file and runs it but I hear no sound. Any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you sure it finds the file? (Does .Play() kill your whole program if the file isn't found? I don't see any other checks.)

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If the sound isn't found, or has the wrong format it kills the program. Rather throws an unhandled exception with an error message. So I guess it does found the file. I also run it from the destination it is compiled/created, so there is no reason not to find the file.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Maybe your call to LoadAsync doesn't complete by the time you try to play the sound.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Maybe not the problem, but your relative path is dependent on the working directory. I suggest using something like:
    Code:
    var SoundFileName = System.IO.Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "Sounds\\Sword.wav");
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That crashes the program. I gives:
    @"C:\Documents and Settings\Alx\My Documents\Visual Studio 2008\Projects\dd\dd\bin\Debug\dd.exe\Sounds\Sword.wav";

    But in any case I replace the above and put the exact location:
    @"C:\Documents and Settings\Alx\My Documents\Visual Studio 2008\Projects\dd\dd\bin\Debug\Sounds\Sword.wav";

    And get the same thing. No sound

    I changed the LoadAsync() and the I get the same thing I again. I can see that it doesn't loads asynchronously (it has a delay), put I can hear no sound!

    I really have no idea why.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by valaris View Post
    Maybe your call to LoadAsync doesn't complete by the time you try to play the sound.
    It is called once actually at an "initialization point". So it has enough time I guess.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Fixed. With the old good method. Restart Windows. Now the firewall asks tells me that the program tries to access the Service Manager. If I block it doesn't play the sound, but runs normally. If I don't block then it runs normally with sound.
    Before it didn't ask me (ah, ah, windows....)

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Before it didn't ask me (ah, ah, windows....)
    It probably did, just so long ago that you don't remember! At least if you are like me and don't reboot the machine unless absolutely NECESSARY.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Maybe, mabye. Yeah, the desktop never closes. And by restart I meant "log off" . Which is extremely usefull option to restore all kind of bugs. It kills everything and begins again without requiring waiting 2min for reboot.

    A waste of energy because we are bored to open/close the PC. Shame on us

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    As for the faulty path, I forgot the call to System.IO.Path.GetDirectoryName on Location.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 12:58 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 03:33 PM
  4. playing sound under water
    By scott27349 in forum Tech Board
    Replies: 9
    Last Post: 01-26-2003, 02:19 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21