-
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...
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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)
-
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.
-
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.
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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?
-
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.
-
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.
-
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?
-
Re: My.Computer.Audio.Play() fails
Also which OS? Both of my dev machines are on 10.
-
Re: My.Computer.Audio.Play() fails
Um, adding the wave file to the project is not possible with arbitrary files somewhere on the system.
-
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.
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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.
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
Also which OS? Both of my dev machines are on 10.
Windows 11 23H2
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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.
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
*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. :)
-
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.
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Re: My.Computer.Audio.Play() fails
-
Re: My.Computer.Audio.Play() fails
Quote:
Originally Posted by
lbrandewie
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.