How can I use OpenAL in .NET Core? - c#

I'm trying to play a wav file from a .NET Core console application (No, I'm not using the console beep. Weird requirements, I know.). I figured I could do that using OpenAL. I'm able to read the file in fine, but then when I try to play it, it fails on the first line:
int handle = AL.GenBuffer();
I get the exception: InvalidOperationException: Could not load openal32.dll. I'm using the OpenTK.NETCore NuGet package. Am I missing something?

I figured it out. It turns out the OpenTK.NETCore package doesn't come with OpenAL, just the SDK. Instead, it looks for it already installed on the machine. I used the installer, and imagine I'll need to install it on every machine that uses my application

Not using OpenAL, but an alternative solution to play wav files in a console application can be found here:
Playing sounds on Console - C#
After adding the sound file to your solution the suggested code in the link is:
using System.Reflection;
using System.IO;
using System.Resources;
using System.Media;
using System.Diagnostics;
namespace Yournamespace
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Assembly assembly;
Stream soundStream;
SoundPlayer sp;
assembly = Assembly.GetExecutingAssembly();
sp = new SoundPlayer(assembly.GetManifestResourceStream
("Yournamespace.Dreamer.wav"));
sp.Play();
}
}
}

Related

Exceptions Using Microsoft.Speech SpeechSynthesizer

I'm working on a speech synthesis project, and I decided to try and use the Microsoft.Speech namespace instead of the built-in System.Speech namespace because Microsoft isn't fixing the memory leak here and recommends using Microsoft.Speech as a workaround.
When I run the program below, I get a NullReferenceException when it calls GetInstalledVoices.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Speech.Synthesis;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.GetInstalledVoices();
}
}
}
And when I run this next program, I get a UnauthorizedAccessException (I am running as an administrator) when it calls Speak.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Speech.Synthesis;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.Speak("exception");
}
}
}
I'm running VS Express 2012 on Windows 8 x64, and the project is configured for x64. I installed the x64 runtime and SDK for Microsoft speech, and installed the en-us language pack from http://www.microsoft.com/en-us/download/details.aspx?id=27224. I even tried downloading the x86 runtime and SDK and changing my project to x86, but that results in a PlatformNotSupportedException.
Is there some other install I'm missing, or is the Microsoft.Speech namespace just not supported on my platform? If I change using Microsoft.Speech.Synthesis to using System.Speech.Synthesis, it's fine except for the memory leak that I mentioned, and I can probably get away with that for now, since this is a hobby application, not for work.
It takes me some time but i realized that i installed only MSSpeech_SR_en-US_TELE.msi which means SpeechRecognition. You need to scroll down in installer and install also text to speech e.g. "MSSpeech_TTS_en-US_Helen.msi".
I'm using eSpeak instead, and just shelling out to their command line program from my .Net program. This is a better solution for me because eSpeak and it's associated voice are easy to install on multiple computers - if I used the Microsoft Speech solutions, I would be stuck with whatever the default voices on that computer are, unless we bought voices for each computer. It also happens that the robotic-sounding eSpeak voice is a better fit for my project, because guess what, it's a talking robot head!
I was having the same problem and noticed that it was a first-time-run problem. So what I did to solve this, is I have a List<InstalledVoice> InstalledVoices; declared as a global property.
Then in the Form.Load(), I have this:
while (InstalledVoices == null)
{
InstalledVoices = SpeechSynth.GetInstalledVoices().ToList();
}
When I ran the debug output on that, it failed once, then succeeded the second time.
That guarantees that you have a collection of the Installed Voices and no null reference. SpeechSynth is my instance of the SpeechSynthesizer class. I store each InstalledVoice in a Dictionary<string, VoiceInfo> for later reference.
Make sure you that Windows updates have been installed.
I tried to get away with a Windows 7 installation without any updates, and something like SpeechSynthesizer.SelectVoice(SomeVoiceName) would fail.
The only solution was to get the automatic Windows updates. Not sure which update exactely resolved the issue.
But I stumble over this problem again and again when I test my app in a VM with Windows 7 without updates.

64bit Type Library and 32bit type libraries out of Sync

This problem has been causing me a headache for a few days, and I cannot find a reason for it. I'm pretty sure this is an environmental issue particular to my machine, but still its causing me problems with testing.
I'm creating a DLL in C# using Visual Studio 2010 Professional. Version one is below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestCOM
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
[System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
public class Class1
{
public void showMessage()
{
MessageBox.Show("Hello from TextCom");
}
}
}
This assembly compiles fine, and everything is good. I run the following script to register it as a COM object (first for 32-bit, then for 64-bit);
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM32.tlb
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM64.tlb
And then use the following script to test it;
dim tc
set tc = CreateObject("TestCOM.Class1")
tc.showMessage()
I use csript to test the script, so I can control which bit depth it uses - I test it once with 32-bit and once with 64-bit. So far everything is good.
Now, when I modify the original assembly to add a function, as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestCOM
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
[System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
public class Class1
{
public void showMessage()
{
MessageBox.Show("Hello from TextCom");
}
public void HelloWorld()
{
MessageBox.Show("Hello World!!");
}
}
}
Before the modification, I unregistered the library using "regasm /unregister" and it reported all types unregistered successfully.
When i register the library, now with changes, the original test script works perfectly. If I extend the test script to call the new HelloWorld function;
In 32-bit scripts, it works perfectly.
In 64-bit scripts, it complains that no such function exists for the TestCOM.Class1 object
I've tried this every which way I can, but I cannot identify why the new function is available to the 32-bit callers, but not the 64-bit calls.
What am I doing wrong ? is there a cache somewhere for the 64-bit stuff I'm not aware of, or a registry setting that needs changed?
To be clear;
1. Build assembly
2. Register using regasm, once for 32 and once for 64
3. Test using script - everything works
4. Unregister library
5. Make modifications, rebuild
6. Register as per Step 2
7. Tests work in 32-bit, but not 64. Wtf ?
Clearly you are suffering from DLL Hell, always around with COM, it is loading an old version of your DLL. Your GAC could have gotten polluted by earlier experiments, it will always find the GACed version first. You are making it worse by specifying the [Guid], making your new class look the same as the old one, even though it is not identical. Preventing COM from telling you that it cannot find the new version of the class.
The most reliable, although noisy, way to see where the DLL came from is by using SysInterals' ProcMon utility. You'll see it reading the registry key and loading the DLL. You can see what directory it came from. Make sure it is not the GAC, remove it with gacutil /u if that's the case, and make sure that you got it rebuilt by checking the timestamp on the file.

AForge.Video.FFMPEG used in C#

I use Visual C# 2008 and want to write AVI file from bmp sequences.
I found AForge.Video.VWF but it's just for "vmw3" or "DIB " codecs and I want to use AForge.Video.FFMPEG but it got error.
For example I just code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Video.FFMPEG;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
VideoFileWriter vfw = new VideoFileWriter();
}
}
}
But I got this filenotfoundexception
{"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null}
In order to use AForge.Video.FFMPEG correctly, you have to be sure to include the FFmpeg dll's into your output folder. The easiest way is to add them into your VS project, go on their properties and set the Copy to Output Directory option to "Always".
The FFmpeg binaries used by AForge can be found on the AForge.NET's external folder, typically in C:\Program Files (x86)\AForge.NET\Framework\Externals\ffmpeg\bin
Not sure if this is still relevant, but downloading Accord.Video.FFMPEG will take care of this for you, just remember to build explicitly to x86
using Accord.Video.FFMPEG;
it's happened because this AForge.Video.FFMPEG.DLL depend upon another dlls. copy all that dll into output folder.

Windows Store Project - Assembly reference error when trying to run cmd commands from a C# file in VS2012

I'm doing some exercises with C# in the trial version of VS 2012. I want to execute a cmd command from a CS file. For this, I've tried Process.Start as well as System.Diagnostics.Process that are mentioned in these posts:
Run Command Prompt Commands
Execute CMD command from code
However, despite I added "using System.Diagnostics" and "using System.ComponentModel", I'm still getting "The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics', missing assembly reference" error. ¿Any suggestion so I can i get rid of this error? Thanks in advance.
This usually happens when you have Target framework = .NET Framework Client Profile, but DLL you reference is from .NET Framework (full). Make sure you have System.dll in your references from valid framework.
I just did the same - created empty console application with the following code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var prc = Process.Start("explorer.exe");
}
}
}
Works perfectly fine for me.
Additional thing to check is Intellisense - when you start typing "System.Diagnostics.Proc"... - does it show you dropdown with "Process" there?
UPDATE:
Windows Store projects are based on different version of target .NET Framework - .NET for Windows Store apps, which does not support functionality you need.
For more details do web search:".NET for Windows Store apps". Helpful links:
http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx

Does not contain a static 'main' method suitable for an entry point

I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
My header look like this in four files, including my main core file that calls:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class shell : Form
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
Does not contain a static 'main' method suitable for an entry point
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
I've looked around so far without a solution.
The properties on each .cs file are set to 'Compile'.
I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
I'm still very new and this is my first attempt at an organizing method with c# code.
I was looking at this issue as well, and in my case the solution was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)
All I needed to do was change the output type of the project properties to Class library
Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.
I had this error and solved it using this solution.
Right click on the project
Select "Properties"
Set "Output Type" to "Class Library".
Try adding this method to a class and see if you still get the error:
[STAThread]
static void Main()
{
}
If you don't have a file named Program.cs, just add a new Class and name it Program.cs.
Then paste this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sales {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.
Had this problem in VS 2017 caused by:
static async Task Main(string[] args)
(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)
Adding
<LangVersion>latest</LangVersion>
to app.csproj helped.
Edit .csproj file
<OutputType>Library</OutputType>
cheers !
If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".
For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error.
To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.
hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main()
I hope it helps..
Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
Salaam,
I have both Visual Studio 2017 and Visual Studio 2019
Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.
Visual Studio 2017
Visual Studio 2019
Just right click on project and select properties and then set Output type on Class Library
After placing the above code in Program.cs, follow below steps
Right click on the project
Select Properties
Set Output Type to Windows Application
Startup object : namepace.Program
When you want to allow paramaters to be specified from the command, they must look like this:
[STAThread]
static void Main(params string[] paramaters)
{
you cannot specify more than one paramater, otherwise this will also cause the error reported above.
For some others coming here:
In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems> without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly
hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
A valid entry looks like:
public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}
I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.
My entry point was placed in /App_Code/Main.cs, and I had to do the following:
Set Project -> Properties -> Application -> Output type = Console Application
Create the /App_Code/Main.cs
Add the code above in it (and reference the methods in my project)
Right click on the Main.cs file -> Properties -> Build Action = Compile
After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.
Why I did this instead of 2 separate projects?
Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.
For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.
If you are using a class library project then set Class Library as output type in properties under application section of project.
Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs. The value for Build Action should be C# compiler.
I accidentally changed Build Action to None, which removed program.cs from the project and therefore wasn't included when compile started.
Did you accidentally remove the entire Program.cs file?
If you have removed,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This might work for you.
Make sure you are not using void with async like
static async void Main(string[] args)
If yes, then change void to Task like
static async Task Main(string[] args)
If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.
For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.
Check to see if the project is set as the "Startup Project"
Right click on the project and choose "Set as Startup Project" from the menu.
If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...
namespace ClassLibrary1
{
public class Class1
{
}
}
To this...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.

Categories

Resources