How can I create an audio context with OpenAL in monodroid? - c#

Whenever I try to create a new audio context in monodroid, it fallls over telling me that openal32.dll is missing; which naturally doesn't make sense, being as this is monodroid, and openal is included as part of OpenTK.
I can only assume i'm doing something wrong, but i've used OpenTK/OpenAL before under windows and have managed just fine with similar code.
anyway here is the offending code:
public Audio()
{
if (Loaded == false)
{
Loaded = true;
try
{
try {Console.WriteLine ("Current: " + AudioContext.CurrentContext.ToString());} catch { Console.WriteLine ("no current context"); }
try {Console.WriteLine ("Default: " + AudioContext.DefaultDevice);} catch { Console.WriteLine ("no default device"); }
Context = new AudioContext();
ValidContext = true; //we have a valid audio context!
}
catch (Exception ex)
{
Console.WriteLine ("Loading the audio context failed.");
Console.WriteLine (ex.ToString ());
Console.WriteLine (ex.Message);
ValidContext = false; //Loading the audio context failed :(
}
}
}
And here is the output:
I/monodroid-gc(24762): environment supports jni NewWeakGlobalRef
D/AndroidGameView(24762): SurfaceCreated
D/libEGL (24762): egl.cfg not found, using default config
D/libEGL (24762): loaded /system/lib/egl/libGLES_android.so
I/mono-stdout(24762): no current context
I/mono-stdout(24762): no default device
I/mono-stdout(24762): Loading the audio context failed.
I/mono-stdout(24762): System.TypeInitializationException: An exception was thrown by the type initializer for OpenTK.Audio.AudioContext ---> System.TypeInitializationException: An exception was thrown by the type initializer for OpenTK.Audio.AudioDeviceEnumerator ---> System.DllNotFoundException: openal32.dll
I/mono-stdout(24762): at (wrapper managed-to-native) OpenTK.Audio.OpenAL.Alc:MakeContextCurrent (intptr)
I/mono-stdout(24762): at OpenTK.Audio.OpenAL.Alc.MakeContextCurrent (ContextHandle context) [0x00000] in <filename unknown>:0
I/mono-stdout(24762): at OpenTK.Audio.AudioDeviceEnumerator..cctor () [0x00000] in <filename unknown>:0
I/mono-stdout(24762): --- End of inner exception stack trace ---
I/mono-stdout(24762): at OpenTK.Audio.AudioContext..cctor () [0x00000] in <filename unknown>:0
I/mono-stdout(24762): --- End of inner exception stack trace ---
I/mono-stdout(24762): at Hardware.Audio..ctor () [0x00000] in <filename unknown>:0
I/mono-stdout(24762): An exception was thrown by the type initializer for OpenTK.Audio.AudioContext
I/ARMAssembler(24762): generated scanline__00000177:03545444_00009001_00000000 [159 ipp] (215 ins) at [0x4fc138:0x4fc494] in 4168381 ns

openal32.dll is not included as part of OpenTK. OpenTK just has bindings for OpenAL. Just like opengl32.dll, which is included as part of your graphics card drivers, openal32.dll must be installed through audio card drivers (or a software implementation of it).
After a quick search, it looks like you need to compile a software implementation of OpenAL for Android: http://pielot.org/2010/12/14/openal-on-android/
Edit: It looks like Android prefers to use the newer OpenSL ES. If you've got a bit of time on your hands, you could try to get OpenTK's generator to generate OpenSL ES bindings for you.
This other StackOverflow question has a few other options for using OpenSL.

Related

PlatformNotSupportedException with NAudio WaveOut in Unity

I am trying to play sounds on a secondary soundcard using NAudio in a Unity project.
It works successfuly using the Play button in the editor, but not in the executable build.
Here is the error i get :
System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Windows.Forms.Form..ctor () [0x00000] in <0b0c181c86c84da989ad80998d3a0a84>:0
at NAudio.Wave.WaveWindow.OnMdiChildActivate (System.EventArgs e) [0x00000] in <554b80148c284b99953f098db496ebd4>:0
at (wrapper remoting-invoke-with-check) NAudio.Wave.WaveWindow..ctor(NAudio.Wave.WaveInterop/WaveCallback)
at NAudio.Wave.WaveCallbackInfo.Connect (NAudio.Wave.WaveInterop+WaveCallback callback) [0x00009] in <554b80148c284b99953f098db496ebd4>:0
at NAudio.Wave.WaveOut..ctor (NAudio.Wave.WaveCallbackInfo callbackInfo) [0x0004e] in <554b80148c284b99953f098db496ebd4>:0
at NAudio.Wave.WaveOut..ctor () [0x00014] in <554b80148c284b99953f098db496ebd4>:0
at MainVideoScript.playSound (System.Int32 deviceNumber) [0x0002e] in <0d2c0b09fee74d5099fd910768d58ea9>:0
I am running on Windows 10, and the Target platform=Windows, so why the platform doesn't support what I am trying to do? What can I change?
Here is the code i am trying to run:
string soundPath = System.IO.Path.Combine(Application.streamingAssetsPath, #"clic2.wav");
waveReader = new NAudio.Wave.WaveFileReader(soundPath);
var wave = new NAudio.Wave.WaveOut();
wave.DeviceNumber = deviceNumber;
wave.Init(waveReader);
wave.Play();
I am using 2019.2.8f1
You need to make sure the Api compatibility Level is correct in Edit \ Project Settings \ Player

Find IL instruction by address from exception stacktrace

I have an exception stacktrace from xamarin-android release build. There no filenames and line numbers:
System.NullReferenceException: Object reference not set to an instance
of an object
at Test.Foo (Test.Bar bar) <0x995f8000 + 0x00248> in <filename unknown>:0
at ...
Is it possible to find IL code by address (0x995f8000 + 0x00248) in DLL that triggers this exception?

RestSharp is crashing with Unity3d when running on iPhone

I'm trying to attach RestSharp to Unity3d.
Original RestSharp mono lib works fine in Editor, but on device its crashing with callstack
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: options
at System.Security.Cryptography.X509Certificates.X509CertificateCollection.GetEnumerator () [0x00000] in <filename unknown>:0
at System.Text.RegularExpressions.Regex.validate_options (RegexOptions options) [0x00000] in <filename unknown>:0
at System.Text.RegularExpressions.Regex..ctor (System.String pattern, RegexOptions options) [0x00000] in <filename unknown>:0
at RestSharp.RestClient..ctor () [0x00000] in <filename unknown>:0
at RestSharp.RestClient..ctor (System.String baseUrl) [0x00000] in <filename unknown>:0
That means, exception appears inside of ctor RestClient(string), which is a wrapper around default ctor. And inside of default ctor, there is
public RestClient()
{
#if WINDOWS_PHONE
this.UseSynchronizationContext = true;
#endif
this.ContentHandlers = new Dictionary<string, IDeserializer>();
this.AcceptTypes = new List<string>();
this.DefaultParameters = new List<Parameter>();
// register default handlers
this.AddHandler("application/json", new JsonDeserializer());
this.AddHandler("application/xml", new XmlDeserializer());
this.AddHandler("text/json", new JsonDeserializer());
this.AddHandler("text/x-json", new JsonDeserializer());
this.AddHandler("text/javascript", new JsonDeserializer());
this.AddHandler("text/xml", new XmlDeserializer());
this.AddHandler("*+json", new JsonDeserializer());
this.AddHandler("*+xml", new XmlDeserializer());
this.AddHandler("*", new XmlDeserializer());
this.FollowRedirects = true;
}
At this point, i dont see where next step with Regex ctor appears. Any ideas?
We have encountered the same issue while building a standalone Windows project with Unity 5.2.1. In the editor it all works fine, until you create a build.
I believe the problem has something to do with the 'structuredSyntaxSuffixWildcardRegex' used in the 'AddHandler' method. While I don't know exactly why it fails, I do know a previous version of RestSharp does work for us.
I guess you're also using the latest RestSharp version, so could you give RestSharp v105.1.0 a try?
It seemed to stop functioning from Restshap v105.2.1, I am going through the commits to see a possible culprit. I will fork and fix it if I have found a solution.
Also I must inform you that in our case GZIP decompression also wasn't working due to missing Mono.Posix libraries. This problem occurred when the server returned a gzipped response and the RestClient allowed it. We had to work around that by using 'Fabman08's fix which made decompression configurable.
I have forked the RestSharp repository in an attempt to fix the above and other Unity related issues. It's located at: https://github.com/eamonwoortman/RestSharp.Unity.
Your and other issues have already been fixed and were tested against a 64bit standalone Windows build.
Could you verify if these fixes also work on your iOS project? Make sure you build the RestSharp.Net35.Unity project.

How to use Azure Mobile Services from Xamarin.Forms?

I try to use the Azure Mobile Services from my simple Forms app and it don't work. The last command just run forever. I checked the internet connection with a WebClient and it is okay. The code works fine in a simple Windows console application.
var ms = new MobileServiceClient(
#"https://xxx.azure-mobile.net/",
#"xxx"
);
IMobileServiceTable<TodoItem> todoTable =
ms.GetTable<TodoItem>();
List<TodoItem> items = todoTable
.Where(todoItem => todoItem.Complete == false).ToListAsync().Result;
Edit:
I use Xamarin Studio because I only have indie license. For the debug I tried a Samsung Galaxy S3 and a Huawei Ascend P7, the result was same. I have updated the Xamarin and now it produces an interesting exception if I use async-await to get the result.
Java.Lang.RuntimeException: java.lang.reflect.InvocationTargetException
at --- End of managed exception stack trace ---
at java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
at at dalvik.system.NativeStart.main(Native Method)
at Caused by: java.lang.reflect.InvocationTargetException
at at java.lang.reflect.Method.invokeNative(Native Method)
at at java.lang.reflect.Method.invoke(Method.java:515)
at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859)
at ... 2 more
at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (Android.Views.IMenu) <IL 0x00007, 0x00050>
at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:4151
at at (wrapper dynamic-method) object.fcd55d8e-49be-4c8f-b3a6-37be6bdb988f (intptr,intptr,intptr) <IL 0x00017, 0x0005b>
at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.n_onPrepareOptionsMenu(Native Method)
at at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.onPrepareOptionsMenu(FormsApplicationActivity.java:110)
at at android.app.Activity.onPreparePanel(Activity.java:2612)
at at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:518)
at at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:872)
at at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:243)
at at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780)
at at android.view.Choreographer.doCallbacks(Choreographer.java:593)
at at android.view.Choreographer.doFrame(Choreographer.java:561)
at at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766)
at at android.os.Handler.handleCallback(Handler.java:733)
at at android.os.Handler.dispatchMessage(Handler.java:95)
at at android.os.Looper.loop(Looper.java:136)
at at android.app.ActivityThread.main(ActivityThread.java:5290)
at ... 5 more
Are you sure you are not blocking your UI with a combination of async / await (deadlock)?
Modify your code as follows:
IMobileServiceTable<TodoItem> todoTable = await ms.GetTable<TodoItem>();
List<TodoItem> items = todoTable.Where(todoItem => todoItem.Complete == false).ToListAsync().ConfigureAwait(false);
See here for more Information about this specific problem: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
For further informations about a deadlock, Stephen Clary has an excellent blog about this: http://blog.stephencleary.com/2012/02/async-and-await.html

MonoTouch iOS application can't find files in directory on device - works on simulator

If I run this code on the device, it returns "got it":
if (Directory.Exists (NSBundle.MainBundle.BundlePath + "/ARCSDev")) {
Console.WriteLine ("got it");
} else {
Console.WriteLine ("can't find it");
}
Which means the directory is in the main bundle.
I need to use that file in this method call:
private void generateChart (int chartNumber, string palette)
{
string filePath = NSBundle.MainBundle.BundlePath + "/ARCSDev";
Console.WriteLine("Filepath - " + filePath);
Loader loader = new Loader (filePath);
loader.LoadChart (Convert.ToString (chartNumber));
The above code works fine on the simulator, but not on the device.
I get the following stack trace when the error occurs on the device:
System.InvalidOperationException: Operation is not valid due to the current state of the object
at System.Linq.Enumerable.Max[TileIndexRecord] (IEnumerable`1 source, System.Func`2 selector) [0x00000] in <filename unknown>:0
at MyCompany.Data.Arcs.Loader.ExtractWriteableBitmap (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette, Double dpi, MyCompany.Data.Arcs.Raschts.ChartIndexFile indexFile, MyCompany.Data.Arcs.Raschts.RasterChartFile chartFile) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:571
at MyCompany.Data.Arcs.Loader.GetHiResImage (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:362
at ARCSViewer.SecondViewController.generateChart (Int32 chartNumber, System.String palette) [0x0004e] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:118
at ARCSViewer.SecondViewController.ViewDidAppear (Boolean animated) [0x00007] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:84
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at ARCSViewer.Application.Main (System.String[] args) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Main.cs:17
The directory contains unix executable files and radiance files.
Can anyone explain whats going on? The file is definitely in the right place (the bundle) as I've tested the code that checks for existence with other files that I know to be there as well..
It might be an issue of case sensitivity, the simulator is case-insensitive while the device is case sensitive. Check your files to see if you're accessing all of them with the right case (i.e. not only the directory).
The permissions on the devices are very different (far more restricted) from the simulator. There's a lot of place you do not have access for various reasons (e.g. changing some files would break the application digital signature which would disallow your application from running anymore).
Also if your application does not follow Apple guidelines on where (and when) to store data your application will be rejected (if you target the appstore).
There's a nice article from Xamarin on how to work with the iOS file system.
I solved the problem, several parts of my application use code like below:
fs = new FileStream(fileName, FileMode.Open);
I needed to change every occurrence of FileStream() to the following:
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
As my files are in the bundle and don't need to make use of write operations, this limits the file stream to only read operations and allowed the files to be read in properly.

Categories

Resources