0

I want to play a sound file to play in my console application but the following doesn't work:

My.Computer.Audio.Play("[file path string]", AudioPlayMode.Background)

When hovering over "my computer" it brings brings up the error 'Computer' is not a member of 'JapaneseHelper.My'. And when hovering over Audio.PlayMode it says 'AudioPlayMode' is not declared. It may be inaccessible due to its protection level.

I have searched everywhere for this and can't find anything that shows me how to do it, I've only seen instructions for forms applications (When I try to copy what is done in the form app, it doesn't work either). I've tried a couple of "Imports" such as Import System.IO but they didn't fix the issue. I don't want to open a music player, I want it to be played in the background like My.Computer.Audio.Play should do. Is it because you can't do it for a console application maybe? However I don't understand why a console apps wouldn't be able to do it.

batv1
  • 131
  • 7

1 Answers1

1

Did you make sure that your audio file is a .wav? From the Microsoft documentation it looks like you can only play .wav files. According to the documentation it should be possible to play audio files even in a console app.

  • Agree. You should also mention that OP have to import `Microsoft.VisualBasic.Devices` namespace, then your answer will be complete. – Maciej Los Jul 17 '20 at 11:28
  • @Maciej Los The problem isn't the playing of the file not working, it is that VB won't recognise the line of code. I get the following error popup when trying that import. Imports statement is unnecessary. Namespace or type specified in the Imports 'Microsoft.VisualBasic.Devices' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. – batv1 Jul 17 '20 at 11:43
  • @JawGBoi, you must ensure a reference to `Microsoft.VisualBasic.dll` is added to the project. If it is, you can easily import `Microsoft.VisualBasic.Devices` namespace. – Maciej Los Jul 17 '20 at 11:55
  • @Maciej Los Sorry, I am new to this kind of this. how do I reference the dll file? And where is this file? – batv1 Jul 17 '20 at 11:57
  • @JawGBoi, please read this: [Adding a Reference to a Visual Basic Project](https://learn.microsoft.com/en-us/windows/win32/com/adding-a-reference-to-a-visual-basic-project) and [How to add reference to Microsoft.VisualBasic.dll?](https://stackoverflow.com/questions/21212194/how-to-add-reference-to-microsoft-visualbasic-dll) – Maciej Los Jul 17 '20 at 11:58
  • @Maciej Los I think I am at the right step so far. But which dll is it? None of them match the name exactly. https://imgur.com/XwdWmSz – batv1 Jul 17 '20 at 12:03
  • None of them.. You have to add reference to the 'framework' dll. See second link in my previous comment. – Maciej Los Jul 17 '20 at 12:04
  • @Maciej Los Whoops I see now. However I don't see the "Assemblies" tab at the top left and therefore can't find the right reference. https://imgur.com/nxsO41U – batv1 Jul 17 '20 at 12:08