Results 1 to 24 of 24

Thread: My.Computer.Audio.Play() fails

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    My.Computer.Audio.Play() fails

    Hey Everyone,

    I cannot get sound working in vb.net. The simplest possible code:

    My.Computer.Audio.Play("test.wav", AudioPlayMode.WaitToComplete)

    fails silently if test.wav exists, but produces errors if it doesn't. I've tried the following:

    - placing the code in form_load
    - placing the code on a button

    -vs 2019 and vs 2022
    -.NET 4.8 and 6.0
    - the different versions of VS were on two different computers

    - tried a workaround suggested elsewhere involving using SndPlaySoundA in Winmm.dll. That got me a chimes sound rather than silence but still didn't play test.wav.

    ... and nothing works. The only thing that would seem to be in common would be .NET itself. Does anyone have a clue here? I've been doing audio applications for some time, just got back to working on one and it's completely broken. Help! TIA...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    produces errors if it doesn't.
    What errors? That is diagnostic information and you're asking us to diagnose the issue. NEVER tell us that an error occurs without specifying EXACTLY what and where it occurs.

    This may or may not be the issue but you should pretty much NEVER specify a file name alone. ALWAYS specify the full path of the file. As it is, the app will assume that the file is in the current directory for the process. That will generally be the program folder by default but it doesn't have to be and it may change. If the file is in the same folder as the EXE then specify that. Assuming WinForms:
    Code:
    My.Computer.Audio.Play(IO.Path.Combine(Application.StartupPath, "test.wav"), AudioPlayMode.WaitToComplete)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    The error info is (If you're surprised by this I'll buy you a coke):

    System.IO.FileNotFoundException
    HResult=0x80070002
    Message=Please be sure a sound file exists at the specified location.
    Source=System.Windows.Extensions
    StackTrace:
    at System.Media.SoundPlayer.ValidateSoundFile(String fileName)
    at System.Media.SoundPlayer.LoadAndPlay(Int32 flags)
    at System.Media.SoundPlayer.Play()
    at Microsoft.VisualBasic.Devices.Audio.Play(SoundPlayer sound, AudioPlayMode mode)
    at Microsoft.VisualBasic.Devices.Audio.Play(String location, AudioPlayMode playMode)
    at Microsoft.VisualBasic.Devices.Audio.Play(String location)
    at PlayWave.Form1.Form1_Load(Object sender, EventArgs e) in C:\projects\PlayWave\Form1.vb:line 14
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ContainerControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

    I really thought it obvious that if the file weren't there you'd get a "File not found" error. My mistake. Also, I've used the technique of prepending application.startuppath to my sound files, but only when they're supposed to be in the executable directory. The app I'm working on is supposed to play sound files by asssociation, so I'm not doing that.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    The problem, of course, is not the errors if the file isn't found. It's the silent failure to play it when it is.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    The app I'm working on is supposed to play sound files by asssociation, so I'm not doing that.
    I'm not sure what that means. Are you saying that your app is registered as the default app for files with the ".wav" extension? If so, I'm not sure how that's relevant here, so could you explain? If not then what do you mean? As I said, if you specify a file name with no path then it will be assumed that the file is in the current directory for the process. Is that what you want? If so, how is the current directory being set?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    I just tried your code with a WAV file of my own and it worked as expected. I added the file to the project and set it to copy to the output folder, then used an unqualified file name and the sound played as expected. Either something is broken on your system or there's something else going on that you haven't told us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    I don't play WAV files all that much but one thing I have noticed is that, when I have, I need to turn the volume up quite high on my system to hear them. I get voicemails as WAV files attached to emails at work and I have to turn the volume up to hear them and I copied a WAV file from my Windows folder to test your code and I had to turn the volume up to hear that too. If Windows plays WAV files very quietly, maybe your code actually is working and you're just not hearing the sound.

    EDIT: Actually, scratch that. I tried a different file and it was perfectly audible at my usual volume level. That behaviour must not apply to WAV files in general.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    It's interesting that you tried it and it worked as expected, but I tried it on two different computers (as stated) with two versions of VS, and two of .NET, and it didn't work. What version of VS and .net were you using when it worked?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    Also which OS? Both of my dev machines are on 10.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    Um, adding the wave file to the project is not possible with arbitrary files somewhere on the system.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    *Are you saying that your app is registered as the default app for files with the ".wav" extension?*

    I misspoke. The app (tho a GUI) will be called from the command line with arguments that can be wildcards indicating .wav files. However, this is irrelevant. But, you did ask.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    It's interesting that you tried it and it worked as expected, but I tried it on two different computers (as stated) with two versions of VS, and two of .NET, and it didn't work. What version of VS and .net were you using when it worked?
    2022 and .NET Framework 4.8.1. I meant to test .NET 6 as well but didn't. I'll do that later.
    Last edited by jmcilhinney; Nov 8th, 2024 at 03:34 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    Also which OS? Both of my dev machines are on 10.
    Windows 11 23H2
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    Um, adding the wave file to the project is not possible with arbitrary files somewhere on the system.
    True, but that doesn't really matter. At run time, the file is just a file. It's just that adding it to the project means that it will be in the program folder at run time, so specifying an unqualified file name will work, as long as the program folder is the current directory, which it will be by default when debugging.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    *Are you saying that your app is registered as the default app for files with the ".wav" extension?*

    I misspoke. The app (tho a GUI) will be called from the command line with arguments that can be wildcards indicating .wav files. However, this is irrelevant. But, you did ask.
    Yes, that is irrelevant, but I didn't know that until I knew what you were actually talking about. Better too much information than not enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    I do wonder if this is MS "helping us" to decide to be on 11. If so they're out of luck with me.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    I do wonder if this is MS "helping us" to decide to be on 11. If so they're out of luck with me.
    This is irrelevant, and I didn't ask.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    OK, I researched the matter (discovered Rufus) and installed 11 on a machine. The code is still silent. I'm befuddled. The machine has sound.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Does the same thing happen with any WAV file? Try using the SoundPlayer class and see if it behaves the same way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    Yes, it happens with all .wav files. Also, I'm only using .wav's known to have sound in them, and my volume is up. Working on the SoundPlayer thing.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    OK, MS's demo code for the SoundPlayer class is 300+ lines, in C# rather than VB. I tried to use it in a simple fashion and got told the class doesn't exist.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    OK, things are getting really weird now. While the SoundPlayer class isn't working, it caused a "Imports System.Media" line to be put at the front of my code, and NOW My.Computer.Audio.Play() works. I conclude that the auto-import mechanism doesn't work for My.Computer.Audio.Play and you have to manually import System.Media.

    Fun. Thanks, MS. I consider this solved now, but I will bring it to MS's attention also. Thanks for your help.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    25

    Re: My.Computer.Audio.Play() fails

    FYI here's the report I made to MS:

    https://developercommunity.visualstu...a-a27ce95339c3

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: My.Computer.Audio.Play() fails

    Quote Originally Posted by lbrandewie View Post
    I conclude that the auto-import mechanism doesn't work for My.Computer.Audio.Play and you have to manually import System.Media.
    There is no import required. All you're achieving by importing a namespace is allowing yourself to use types from that namespace unqualified in your code. It doesn't give you access to any additional types or the like. It could be the case that some unqualified type is interpreted as coming from one namespace and adding another import would cause it to be interpreted as another, but I would expect that to generate an ambiguous type error at compile time. I can't explain what's going on in your case but I didn't have to import anything when I tested. It would also be interesting to see what happens now if you remove that import. Before doing so, is it greyed out? VS greys out namespace imports that are not being used so, if it is, that's an indication that you're not using any types from that namespace in your code. If it's not greyed out then that suggests that it is being used somewhere, but I don't see how that could be related to a call to My.Computer.Audio.Play.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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