I am trying to go through this tutorial for on making sounds with waves using C#:
https://www.codeguru.com/dotnet/making-sounds-with-waves-using-c/
The first sample code it has you run is this, which is supposed to play a .wav file:
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Devices;
namespace MyApp
{
class Program
{
static Audio myAudio = new Audio();
static void Main()
{
myAudio.Play("m:\crooner2.wav",
AudioPlayMode.WaitToComplete);
}
}
}
In my code, the filepath and name of the .wav file was replaced with a different one, but otherwise the code is identical. However, I get an error regarding the second line of code: error CS0234: The type or namespace name 'Devices' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)
Without the Microsoft.VisualBasic.Devices call working, I have no way of running even the first exercise in the tutorial, and definitely no way of further progressing in using C# for sound manipulation.
I was expecting the code to run and play the .wav file. However, I got the error message instead.
As part of debugging, I came across this post on the Microsoft website:
I’m not sure what to make of it. It seems like it’s saying it could be solved by upgrading to .Net 5 or higher, but I’m already using .Net 5. It also seems like it saying that Microsoft.VisualBasic.Devices was made obsolete with .Net Core 3.0, so I’m not sure how upgrading would make it easier to use something that was made obsolete.
It also says that certain functionality in Microsoft.VisualBasic.Devices has equivalent functionality that can be called by other means. It gives specific replacement calls for Microsoft.VisualBasic.Devices.Clock and Microsoft.VisualBasic.Devices.Ports, but nothing for Microsoft.VisualBasic.Devices.Audio, which is what I want to use in my code.
I have tried this in both Visual Studio and Visual Studio Code and get the same errors either way.
2
Answers
You have to use reference to "Microsoft.VisualBasic" in your project.
You have two way for do that :
1st way :
Right click on project references, select "Assemblys" => "Framework" and you can search (at top right) "basic" keyword, and select "Microsoft.VisualBasic" item.
After that you can compile your project and normally its works !
Screen for add references
2nd way :
Comment or delete "using Microsoft.VisualBasic.Devices;" instruction.
The class "Audio" should be underline to red, hover with your mouse and you have option by IDE which let you "using Microsoft.VisualBasic.Devices;".
References should be automatically added in project.
And now you can compile your project.
With .NET Core, you generally add references via NuGet packages. There is no NuGet package for Microsoft.VisualBasic.Forms.dll, which the documentation clearly states that type is declared in. To get that assembly reference, I believe that you need to replace this line in your project file:
with these lines:
After doing that, you should see
Microsoft.WindowsDesktop.App.WindowsForms
added under Dependencies -> Frameworks in the Solution Explorer and the relevant assembly is listed under that.