C++ Unity Plugin how to release USB audio device - c#

I've created a C++ Unity plugin that sends audio to a speaker via USB.
Everything works fine the first time I hit play on Unity, however, after the first playback if I hit play once more then unity crashes. In addition, I have to occasionally reset my computer to get the plugin working.
I'm thinking that I need to add the proper command to my "OnDestroy" function in my .CS Unity marshaling script. Here's what I have right now...
private void OnDestroy()
{
Audio.stop_sound();
Debug.Log("stop called");
}
stop_sound is a simple function that exhibits a stop to the callback.
Any ideas?

Related

Discord SDK Terminates Unity Editor when Discord is not running

This question doesn't really contain any code...
I Added the Discord SDK to my project and every time I run the game, it Closes the Unity Editor.
Is there a way to stop the Discord SDK from terminating the Editor?
#EliasV you need Discord app running when trying to launch the game in Editor.
/*
Grab that Client ID from earlier
Discord.CreateFlags.Default will require Discord to be running for the game to work
If Discord is not running, it will:
1. Close your game
2. Open Discord
3. Attempt to re-open your game
Step 3 will fail when running directly from the Unity editor
Therefore, always keep Discord running during tests, or use Discord.CreateFlags.NoRequireDiscord
*/
https://discord.com/developers/docs/game-sdk/sdk-starter-guide
For init, you can change the Discord.CreateFlags.Default to Discord.CreateFlags.NoRequireDiscord.
Or, you can use #if to check for Unity Editor to NoRequireDiscord - else, use the Default one!

How to detect BLE beacons on Unity3D HoloLens app?

I have built an app in Unity3D which should be able to detect Bluetooth Low-Energy beacons on the Microsoft HoloLens. Here is the Unity C# script code that I used to get this done.
using UnityEngine;
using Windows.Devices.Bluetooth.Advertisement;
public class BeaconDetector : MonoBehaviour
{
private BluetoothLEAdvertisementWatcher _watcher;
void Start()
{
_watcher = new BluetoothLEAdvertisementWatcher();
_watcher.Received += WatcherOnReceived;
_watcher.Start();
}
//This method should be called when a beacon is detected
void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
//Just a simple check if this method is even called
Debug.Log("Beacon detected!");
}
}
The app builds and runs on the HoloLens nicely, but (even after waiting a couple of minutes) I do not get the desired output line in my debug log. This means to me that the WatcherOnReceived() method is never called, which ultimately means that no beacon is detected.
I use some Sensoro SmartBeacon-4AA's which can transmit both iBeacon and Eddystone signals.
I have been trying this for a couple of weeks right now, did several tutorials along the way but I still can not figure out why this is not working for me.
It was a permission problem after all. It is possible to solve this issue in both Unity (pre-build) and Visual Studio (post-build). Here are both solutions:
Solution A: in Unity
Head to Edit > Project Settings > Player.
In the Inspector, open up the tab Publishing Settings.
Under the Capabilities section, make sure that Bluetooth is enabled.
Click on the .gif to expand
Solution B: in Visual Studio (2017)
In your Solution Explorer (Ctrl+Alt+L), expand your project.
Double-click the Package.appxmanifest.
Go to the Capabilities tab.
Again, make sure that Bluetooth is enabled.
Click on the .gif to expand
This will give your app the right to use Bluetooth on the HoloLens.

How can I run a "headless" (no GUI) Unity3D game inside a C# program?

I would like to execute only the model part of a game, without a GUI of any sort.
As such, I would instantiate some classes from Unity3D, arrange them somehow, "start" them whatever that means, and periodically read the state of the components inside the game, or else receive update events from them.
Currently I have already the Unity DLLs available with a NuGet Package, but I don't know how to go on from here:
public class HelloUnity3D
{
void run()
{
var myGame = new UnityEngine.MonoBehaviour();
//myGame.WhatNow(?)
}
}
No, it's impossible.
AFAIK, MonoBehaviour is executed by the editor or players (playback engine) of Unity3D-you can find them in unity3d_install_dir\Editor\Data\PlaybackEngines on Windows.
So if you want to "execute only the model part of a game", you have to write a modeling-only playback engine. But I don't think that can be done without access to source code of Unity3D.
But it's possible to run MonoBehaviour codes that doesn't run code that requires Unity3D playback engines.

Playing Stream Audio with MPMoviePlayerController in Background Modes

my first question here...
I have been searching how solve this issue i start a app using monotouch creating my MPMoviePlayerController like this:
this.mp = new MPMoviePlayerController(new NSURL("http://stream3.dyndns.org:1935/iphone/xeco.stream/playlist.m3u8"));
this.mp.useApplicationAudioSession=false //also try with true
this.mp.PrepareToPlay();
this.mp.Play();
Everything works great, i hear the audio
Notes:
I already search how fix this and on my plist file add UIBackgroundModes and string audio as many members advice. (Using the plist editor provided in monotouch). Also add this code to set the category of my audio session:
AudioSession.Initialize();
AudioSession.Interrupted += delegate{
Console.Writeline("interrupted");
}
AudioSession.Category = AudioSessionCategory.MediaPlayback; //also try ambientsound and others
The problem is:
How can i keep the audio playing when the app goes background, when the device blocks or the home button is pushed?
The stream always seems get muted when press home or block the device, when i return to the app the audio starts very quickly wich make me think that the app is streaming all the time but only doesnt hear.
I am testing only on the Iphone Simulator, i am starting to think that maybe this is caused only on simulator. any advice?
thanks in advance.
UPDATE:
I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false
I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false
The app works great on the device. Monotouch rules!

System.Media.SoundPlayer.Play() freezes GUI

I am trying to implement a simple game in .NET with C#. There is a ball bouncing against objects, and when it bounces I play a sound asynchronously using System.Media:SoundPlayer. The problem is that most of the time (not always) the ball freezes at the time of impact, for as long as the sound plays.
This happens on Windows XP, but not on Windows 7. I use .NET 3.5.
And the weirdest thing is that the problem disappears if I open the application Windows Media Player and play a few seconds of an mp3.
Here's the code where I initialize the SoundPlayer object:
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("Game.Resources.GameBounce.wav");
_playerBounce = new System.Media.SoundPlayer(s);
_playerBounce.Load();
...and when the ball hits an obstacle, this line is called:
_playerBounce.Play();
I have also tried "Attempt #4" in this post:
How to use System.Media.SoundPlayer to asynchronously play a sound file?
Another desperate attempt, based on some advice I found somewhere on the net, was to save the sound stream to a file, that SoundPlayer works better when initialized from a file.
Neither of those attempts worked.
Any ideas?

Categories