NB: The usual blog disclaimer for this site applies to posts around HoloLens. I am not on the HoloLens team. I have no details on HoloLens other than what is on the public web and so what I post here is just from my own experience experimenting with pieces that are publicly available and you should always check out the official developer site for the product documentation.
A quick note – I’m still not sure whether I should bring this blog back to life having paused it but I had a number of things running around in my head that are easier if written down and so that’s what I’ve done
Viewing 3D Files on a HoloLens
A few weeks ago, a colleague came to me with 2 3D models packaged in files and said “I just want to show these 2 models to a customer on a HoloLens”.
I said to him;
“No problem, open up the files in 3D Viewer on the PC, have a look at them and then transfer them over to HoloLens and view them in 3D Viewer there”
Having passed on this great advice, I thought I’d better try it out myself and, like much of my best advice, it didn’t actually work
Here’s why it doesn’t work. I won’t use the actual models in this blog post so let’s assume that it was this model from Remix3D;
Now, I can open that model in Paint3D or 3D Viewer, both of which are free and built-in on Windows 10 and I can get a view something like this one;
which tells me that this model is 68,000 polygons so it’s not a tiny model but it’s not a particularly big one either and I’d expect that it would display fine on a mobile device which might not be the case if it was 10x or 100x times as big.
Now, knowing that there’s an application on my PC called “3D Viewer” and knowing that there’s one on my HoloLens called “3D Viewer” might lead me to believe that they are the same application with same set of capabilities and so I might just expect to be able to move to the HoloLens, run the Mixed Reality Viewer application and open the same model there.
But I can’t.
3D Viewer on PC
If you run up the 3D Viewer on a PC then you get an app which runs in a Window and which displays a 3D model with a whole range of options including being able to control how the model is rendered, interacting with animations, changing the lighting and so on;
The application lets you easily load up model files from the file system or from the Remix3D site;
You can also use this application to “insert” the model into the real-world via a “Mixed Reality” mode as below;
I’d say that (for me) this is very much at the “Augmented Reality” end of the spectrum in that while the model here might look like it’s sitting on my monitor, I can actually place it in mid-air so I’m not sure that it’s really identifying planes for the model to sit on. I can pick up my laptop and wander around the model and that works to some extent although I find it fairly easy to confuse the app.
One other thing that I’d say in passing is that I have no knowledge around how this application offers this experience or how a developer would build a similar experience – I’m unaware of any platform APIs that help you build this type of thing for a PC using a regular webcam in this way.
3D Viewer on HoloLens
3D Viewer on HoloLens also runs in a window as you can see here;
and you can also open up files from the file system or from the Remix3D site or from a pre-selected list of “Holograms” which line up with the content that used to be available in the original “Holograms” app going all the way back to when the device was first made available.
The (understandable) difference here is that when you open a model, it is not displayed as a 3D object inside of the application’s Window as that would be a bit lame on a HoloLens device.
Instead, the model is added to the HoloLens shell as shown below;
This makes sense and it’s very cool but on the one hand it’s not really an immersive viewing application – it’s a 2D application which is invoking the shell to display a 3D object.
As an aside, it’s easy to ask the Shell to display a 3D object using a URI scheme and I wrote about that here a while ago and I suspect (i.e. I don’t know) that this is what the 3D Viewer application is doing here;
Placing 3D Models in the Mixed Reality Home
The other aspect of this is that 3D models displayed by the Shell have requirements;
and so you can’t just display an arbitrary model here and I tend to find that most models that I try and use in this way don’t work.
For example, if we go back to the model of a Surface Book 2 that I displayed in 3D Viewer on my PC then I can easily copy that model across to my HoloLens using the built-in “Media Transfer Protocol” support which lets me just see the device’s storage in Explorer once I’ve connected it via USB and then open it up in 3D Viewer where I see;
and so I find that regardless of their polygon count most, general models, don’t open within the 3D Viewer on HoloLens – they tend to display this message instead and that’s understandable given that the application is trying to;
- do the right thing by not having the user open up huge models that won’t then render well
- integrate the models into the Shell experience which has requirements that presumably can’t just be ignored.
So, if you want a simple viewer which just displays an arbitrary model in an immersive setting then 3D Viewer isn’t quite so general purpose.
This left me stuck with my colleague who wanted something simple to display his models and so I made the classic mistake.
I said “I’ll write one for you”
This Does Not Solve the Large/Complex Models Problem
I should stress that me setting off to write a simple, custom viewer is never going to solve the problem of displaying large, complex 3D models on a mobile device like a HoloLens and, typically, you need to think about some strategy for dealing with that type of complexity on a mobile device. There are guidelines around this type of thing here;
Performance recommendations for HoloLens apps
and there are tools/services out there to help with this type of thing including tools like;
My colleague originally provided me with a 40K polygon model and a 500K polygon model.
I left the 40K model alone and used 3DS Max to optimise the 500K poly model down to around 100K which rendered fine for me on HoloLens through the application that I ended up building.
It took a bit of experimentation in the different tools to find the right way to go about it as some tools failed to load the models, others produced results that didn’t look great, etc. but it didn’t take too long to decimate the larger one.
Building glTF Viewer Version 1.0
So, to help out with the promise I’d made to my colleague, I built a simple app. It’s in the Windows Store over here and the source for it is on Github over here.
It’s currently heading towards Version 2.0 when I merge the branches back together and get the Store submission done.
For version 1.0, what I wanted was something that would allow a user to;
- open a 3D file in .GLB/.GLTF format from their HoloLens storage.
- display the 3D model from it.
- manipulate it by scaling, rotating and translating.
- have as little UI as possible and drive any needed interactions through speech.
and that was pretty much all that I wanted – I wanted to keep it very simple and as part of that I decided to deliberately avoid;
- anything to do with other 3D model file formats but was, instead, quite happy to assume that people would find conversion tools (e.g. Paint3D, 3D Builder, etc) that could generate single file (.GLB) or multi-file (.GLTF) model files for them to import.
- any attempt to open up files from cloud locations via OneDrive etc.
With that in mind, I set about trying to build out a new Unity-based application and I made a couple of quick choices;
- that I would use the Mixed Reality Toolkit for Unity and I chose to use the current version of the Toolkit rather than the vNext toolkit as that’s still “work in progress” although I plan to port at a later point.
- this meant that I could follow guidance and use the LTS release of Unity – i.e. a 2017.4.* version which is meant to work nicely with the toolkit.
- that I would use UnityGLTF as a way of reading GLTF files inside of Unity.
- that I would use sub-modules in git as a way of bringing those two repos into my project as described by my friend Martin over here.
I also made a choice that I would use standard file dialogs for opening up files within my application. This might seem like an obvious choice but those dialogs only really work nicely once your HoloLens is running on the “Redstone 5” version of Windows 10 as documented here;
and so I was limiting myself to only running on devices that are up-to-date but I don’t think that’s a big deal for HoloLens users.
In terms of how the application is put together, it’s a fairly simple Unity application using only a couple of features from the Mixed Reality Toolkit beyond the base support for cameras, input etc.
- Speech commands – I have commands for “open” and “reset” and I use SpeechInputSource and SpeechInputHandler for this.
- Manipulations – I use the TwoHandManipulatable script for this.
Generally, beyond a few small snags with Unity when it came to generating the right set of assets for the Windows Store I got that application built pretty quickly & submitted it to the Store.
However, I did hit a few small challenges…
A Small Challenge with UnityGLTF
I did hit a big of a snag because the Mixed Reality Toolkit makes some use of pieces from a specific version of UnityGLTF to provide functionality which loads the Windows Mixed Reality controller models when running on an immersive headset.
UnityGLTF (scripts and binaries) in the Mixed Reality Toolkit
I wanted to be able to bring all of UnityGLTF (a later version) into my project alongside the Mixed Reality Toolkit and so that caused problems because both scripts & binaries would be duplicated and Unity wasn’t very happy about that
I wrote a little ‘setup’ script to remove the GLTF folder from the Mixed Reality Toolkit which was ok except it left me with a single script named MotionControllerVisualizer.cs which wouldn’t build because it had a dependency on UnityGLTF methods that were no longer part of the Unity GLTF code-base (i.e. I happened to have the piece of code which seemed to have an out-of-date dependency).
That was a little tricky for me to fix so I got rid of that script too and fixed up the scripts that took a dependency on it by adding my own, mock implementation of that class into my project knowing that nothing in my project was ever going to display a motion controller anyway.
It’s all a bit “hacky” but it got me to the point where I could combine the MRTK and UnityGLTF in one place and build out what I wanted.
A Small Challenge with Async/Await and CoRoutines
One other small challenge that I hit while putting together my version 1.0 application is the mixing of the C# async/await model with Unity’s CoRoutines.
I’ve hit this before and I fully understand where Unity has come from in terms of using CoRoutines but it still bites me in places and, specifically, it bit me a little here in that I had code which was using routines within the UnityGLTF which are CoRoutine based and I needed to get more information around;
- when that code completed
- what exceptions (if any) got thrown by that code
There’s a lot of posts out there on the web around this area including these examples;
and in my specific case I had to write some extra code to try and glue together running a CoRoutine, catching exceptions from it and tying it into async/await but it wasn’t too challenging, it just felt like “extra work” that I’m sure in later years won’t have to be done as these two models get better aligned. Ironically, this situation was possibly more clear-cut when async/await weren’t really available to use inside of Unity’s scripts.
Another Small Challenge with CoRoutines & Unity’s Threading Model
Another small challenge here is that the UnityGLTF code which loads a model needs to, naturally, create GameObjects and other UI constructs inside of Unity which aren’t aren’t thread-safe and have affinity to the UI thread. So, there’s no real opportunity to run this potentially expensive CoRoutine on some background thread but, rather, it hogs the UI thread a bit while it’s loading and creating GameObjects.
I don’t think that’s radically different from other UI frameworks but I did contemplate trying to abstract out the creation of the UI objects so as to defer it until same later point when it could all be done in one go but I haven’t attempted to do that and so, currently, while the GLTF loading is happening my UI is displaying a progress wheel which can miss a few updates
Building glTF Viewer Version 2.0
Having produced my little Version 1.0 app and submitted it to the Store, the one thing that I really wanted to add was the support for a “shared holographic experience” such that multiple users could see the same model in the same physical place. It’s a common thing to want to do with HoloLens and it seems to be found more in large, complex, enterprise apps than in just simple, free tools from the Store and so I thought I would try and rectify that a little.
In doing so, I wanted to try and keep any network “infrastructure” as minimal as possible and so I went with the following assumptions.
- that the devices that wanted to share a hologram were in the same space on the same network and that network would allow multicast packets.
- sharing is assumed in the sense that the experience would automatically share holograms rather than the user having to take some extra steps.
- that not all the devices would necessarily have the files for the models that are loaded on the other devices.
- that there would be no server or cloud connectivity required.
The way in which I implemented this centres around a HoloLens running the glTF Viewer app acting as a basic web server which serves content out of its 3D Objects folder such that other devices can request that content and copy it into their own 3D Objects folder.
The app then operates as below to enable sharing;
- When a model is opened on a device
- The model is given a unique ID.
- A list of all the files involved in the model is collected (as GLTF models can be packaged as many files) as the model is opened.
- A file is written to the 3D Objects folder storing a relative URI for each of these files to be obtained remotely by another device.
- A spatial anchor for the model is exported into another file stored in the 3D Objects folder.
- A UDP message is multi-casted to announce that a new model (with an ID) is now available from a device (with an IP address).
- The model is made so that it can be manipulated (scale, rotate, translate) and those manipulations (relative to the parent) are multi-cast over the network with the model identifier attached to them.
- When a UDP message announcing a new model is received on a device
- The device asks the user whether they want to access that model.
- The device does web requests to the originating device asking for the URIs for all the files involved in that model.
- The device downloads (if necessary) each model file to the same location in its 3D Objects folder.
- The device downloads the spatial anchor file.
- The device displays the model from its own local storage & attaches the spatial anchor to place it in the same position in the real world.
- The model is made so that it cannot be manipulated but, instead, picks up any UDP multicasts with update transformations and applies them to the model (relative to its parent which is anchored).
and that’s pretty much it.
This is all predicated on the idea that I can have a HoloLens application which is acting as a web server and I had in mind that this should be fairly easy because UWP applications (from 16299+) now support .NET Standard 2.0 and HttpListener is part of .NET Standard 2.0 and so I could see no real challenge with using that type inside of my application as I’d written about here;
UWP and .NET Standard 2.0–Remembering the ‘Forgotten’ APIs 🙂
but there were a few challenges that I met with along the way.
Challenge Number 1 – Picking up .NET Standard 2.0
I should say that I’m long past the point of being worried about being seen to not understand something and am more at the point of realising that I don’t really understand anything
I absolutely did not understand the ramifications of wanting to modify my existing Unity project to start making use of HttpListener
Fairly early on, I came to a conclusion that I wasn’t going to be able to use HttpListener inside of a Unity 2017.4.* project.
Generally, the way in which I’ve been developing in Unity for HoloLens runs something like this;
- I am building for the UWP so that’s my platform.
- I use the .NET scripting backend.
- I write code in the editor and I hide quite a lot of code from the editor behind ENABLE_WINMD_SUPPORT conditional compilation because the editor runs on Mono and it doesn’t understand the UWP API surface.
- I press the build button in Unity to generate a C#/.NET project in Visual Studio.
- I build that project and can then use it to deploy, debug my C#/UWP application and generate store packages and so on.
It’s fairly simple and, while it takes longer than just working in Visual Studio, you get used to it over time.
One thing that I haven’t really paid attention to as part of that process is that even if I select the very latest Windows SDK in Unity as below;
then the Visual Studio project that Unity generates doesn’t pick up the latest .NET packages but, instead, seems to downgrade my .NET version as below;
I’d struggled with this before (in this post under the “Package Downgrade Issue”) without really understanding it but I think I came to a better understanding of this as part of trying to get HttpListener into my project here.
In bringing in HttpListener, I hit build problems and I instantly assumed that I needed to upgrade Unity because Unity 2017.* does not offer .NET Standard 2.0 as an API Compatibility Level as below;
and I’d assumed that I’d need to move to a Unity 2018.* version in order to pick up .NET Standard 2.0 as I’d seen that Unity 2018.* had started to support .NET Standard 2.0.
Updated scripting runtime in Unity 2018.1: What does the future hold?
and so needing to pick up a Unity 2018.* version and switch in there to use .NET Standard 2.0 didn’t surprise me and so I got version 2018.2.16f1 and I opened up my project in there and switched to .NET Standard 2.0 and that seemed like a fine thing to do;
but it left me with esoteric build failures as I hadn’t realised that Unity’s deprecation of the .NET Scripting Backend as per this post;
Deprecation of support for the .Net Scripting backend used by the Universal Windows Platform
had a specific impact in that it meant that new things which came along like SDK 16299 with its support for .NET Standard 2.0 didn’t get implemented in the .NET Scripting Backend for Unity.
They are only present in the IL2CPP backend and I presume that’s why my generated .NET projects have been downgrading the .NET package used.
So, if you want .NET Standard 2.0 then you need SDK 16299+ and that dictates Unity 2018.+ and that dictates moving to the IL2CPP backend rather than the .NET backend.
I verified this over here by asking Unity about it;
2018.2.16f1, UWP, .NET Scripting Backend, .NET Standard 2.0 Build Errors
and that confirms that the .NET Standard 2.0 APIs are usable from the editor and from the IL2CPP back-end but they aren’t going to work if you’re using .NET Scripting Backend.
I did try. I hid my .NET code in libraries and referenced them but, much like the helpful person told me on the forum – “that didn’t work”.
Challenge Number 2 – Building and Debugging with IL2CPP on UWP/HoloLens
Switching to the IL2CPP back-end really changed my workflow around Unity. Specifically, it emphasised that I need to spend as much time in the editor because I find that the two phases of;
- building inside of the Unity editor
- building the C++ project generated by the Unity editor
is a much lengthier process than doing the same thing on the .NET backend and Unity has an article about trying to improve this;
but I didn’t really find that I could get my build times to come down much and I’d find that maybe a one-line change could take me into a 20m+ build cycle.
The other switch in my workflow was around debugging. There are a couple of options here. It’s possible to debug the generated C++ code and Unity has an article on it here;
Universal Windows Platform: Debugging on IL2CPP Scripting Backend
but I’d have to say that it’s pretty unproductive trying to find the right piece of code and then step your way through generated C++ which looks like;
but you can do it and I’ve had some success with it and one aspect of it is “easy” in that you just open the project, point it at a HoloLens/emulator for deployment & then press F5 and it works.
The other approach is to debug the .NET code because Unity does have support for this as per this thread;
and the details are given again in this article;
Universal Windows Platform: Debugging on IL2CPP Scripting Backend
although I would pay very close attention to the settings that control this as below;
and I’d also pay very close attention to the capabilities that your application must have in order to operate as a debuggee. I had to question how to get this working on the Unity Forums;
Unity 2018.2.16f1, UWP, IL2CPP, HoloLens RS5 and Managed Debugging Problems
but I did get it to work pretty reliably on HoloLens in the end but I’d flag a few things that I found;
- sometimes the debugger wouldn’t attach to my app & I’d have to restart the app. It would be listed as a target in Unity’s “Attach To” dialog in Visual Studio but attaching just did nothing.
- that the debugger can be very slow – sometimes I’d wait a long time for breakpoints to become active.
- that the debugger quite often seems to step into places where it can’t figure out the stack frame. Pressing F10 seemed to fix that.
- that the debugger’s step-over/step-into sometimes didn’t seem to work.
- that the debugger’s handling of async/await code could be a bit odd – the instruction pointer would jump around in Visual Studio as though it had got lost but the code seemed to be working.
- that hovering over variables and putting them into the watch windows was quite hit-and-miss.
- that evaluating arbitrary .NET code in the debugger doesn’t seem to work (I’m not really surprised).
- breaking on exceptions isn’t a feature as far as I can tell – I think the debugger tells you so as you attach but I’m quite a fan on stopping on first-chance exceptions as a way of seeing what code is doing.
I think that Unity is working on all of this and I’ve found them to be great in responding on their forums and on Twitter, it’s very impressive.
In my workflow, I tended to use both the native debugger & the managed debugger to try and diagnose problems.
One other thing that I did find – I had some differences in behaviour between my app when I built it with “script debugging” and when I didn’t. It didn’t affect me too much but it did lower my overall level of confidence in the process.
Putting that to one side, I’d found that I could move my existing V1.0 project into Unity 2018.* and change the backend from .NET to IL2CPP and I could then make use of types like HttpListener and build and debug.
However, I found that the code stopped working
Challenge 3 – File APIs Change with .NET Standard 2.0 on UWP
I hadn’t quite seen this one coming. There’s a piece of code within UnityGLTF which loads files;
In my app, I open a file dialog, have the user select a file (which might result in loading 1 or many files depending on whether this is a single-file or multi-file model) and it runs through a variant of this FileLoader code.
That code uses File.Exists() and File.OpenRead() and, suddenly, I found that the code was no longer working for files which did exist and which my UWP app did have access to.
It’s important to note that the file in question would be a brokered file for the UWP app (i.e. one which it accesses via a broker to ensure it has the right permissions) rather than just say a file within the app’s own package or it’s own dedicated storage. In particular, my file would reside within the 3D Objects folder.
How could that break? It comes back to .NET Standard 2.0 because these types of File.* functions work differently for UWP brokered files depending on whether you are on SDK 16299+ with .NET Standard 2.0 or on an earlier SDK before .NET Standard 2.0 came along.
The thorny details of that are covered in this forum post;
File IO operations not working when broadFileSystemAccess capability declared
which gives some of the detail but, essentially, for my use case File.Exists and File.OpenRead were now causing me problems and so I had to replace some of that code which brings me back to…
Challenge 4 – Back to CoRoutines, Enumerators and Async
As I flagged earlier, mixing and matching an async model based around CoRoutines in Unity (which is mostly AFAIK about asynchronous rather than concurrent code) with one based around Tasks can be a bit of a challenge.
With the breaking change to File.OpenRead(), I had to revisit the FileLoader code and modify it such that it still presented an IEnumerator-based pattern to the rest of the UnityGLTF code while, internally, it needed to move from using the synchronous File.OpenRead() to the asynchronous StorageFile.OpenReadAsync().
It’s not code that I’m particularly proud of and wouldn’t like to highlight it here but it felt like one of those situations where I got boxed into a corner and had to make the best of what I had to work with
Challenge 5 – ProgressRings in the Mixed Reality Toolkit
I’m embarrassed to admit that I spent a lot longer trying to get a ProgressRing from the Mixed Reality Toolkit to work than I should have.
I’ve used it before, there’s an example over here;
but could I get it to show up? No.
In the end, I decided that there was something broken in the prefab that makes up the progress ring and I switched from using the Solver Radial View to using the Solver Orbital script to manage how the progress ring moves around in front of the user & that seemed to largely get rid of my problems.
Partially, this was a challenge because I hit it at the time when I was struggling to get used to my new mode of debugging and I just couldn’t get this ring to show up.
In the end, I solved it by just making a test scene and watching how that behaved in the editor at runtime before applying that back to my real scene which is quite often how I seem to solve these types of problems in Unity.
Challenge 6 – UDP Multicasting on Emulators and HoloLens
I chose to use UDP multicasting as a way for one device to notify others on the same network that it had a new model for them to potentially share.
This seemed like a reasonable choice but it can make it challenging to debug as I have a single HoloLens and have never been sure whether a HoloLens emulator can/can’t participate in UDP multicasting or whether there’s any settings that can be applied to the virtual machine to make that work.
I know when I wrote this post that I’d failed to get multicasting working on the emulator and this time around I tried a few combinations before giving up and writing a test-harness for my PC to act as a ‘mock’ HoloLens from the point of view of being able to generate/record/playback messages it received from the real HoloLens.
I’ve noticed over time a number of forum posts asking whether a HoloLens can receive UDP traffic at all such as;
and there are more.
I can certainly verify that a UWP app on HoloLens can send/receive UDP multicast traffic but I’d flag that I have seen situations where my current device (running RS5) has got into a situation where UDP traffic seems to fail to be delivered into my application until I reboot the device. I’ve seen it very occasionally but more than once so I’d flag that this can happen on the current bits & might be worth bearing in mind for anyone trying to debug similar code on similar versions.
I learned quite a lot in putting this little test application together – enough to think it was worth opening up my blog and writing down some of the links so that I (or others) can find them again in the future.
If you’ve landed here via search or have read the whole thing ( ! ) then I hope you found something useful.
I’m not sure yet whether this one-off-post is the start of me increasing the frequency of posting here so don’t be too surprised if this blog goes quiet again for a while but do feel very free to reach out if I can help around these types of topics and, of course, feel equally free to point out where I’ve made mistakes & I’ll attempt to fix them
Update – One Last Thing (Challenge 7), FileOpenPicker, Suspend/Resume and SpeechRecognizer
Finding a Suspend/Resume Problem with Speech
I’d closed off this blog post and published it to my blog and I’d shipped version 2.0 of my app to the Store when I came across an extra “challenge” in that I noticed that my voice commands seemed to be working only part of the time and, given that the app is driven by voice commands, that seemed like a bit of a problem.
It took me a little while to figure out what was going on because I took the app from the Store and installed it and opened up a model using the “open” command and all was fine but then I noticed that I couldn’t then use the “open” command for a second time or the “reset” command for a first time.
Naturally, I dusted the code back off and rebuilt it in debug mode and tried it out and it worked fine.
So, I rebuilt in release mode and I got mixed results in finding that sometimes things worked and other times they didn’t and it took me a while to realise that it was the debugger which was making the difference. With the debugger attached, everything worked as I expected but when running outside of the debugger, I would find that the voice commands would only work until the FileOpenPicker had been on the screen for the first time. Once that dialog had been on the screen the voice commands no longer worked and that was true whether a file had been selected or whether the dialog had simply been cancelled.
So, what’s going on? Why would putting a file dialog onto the screen cause the application’s voice commands to break and only when the application was not running under a debugger?
The assumption that I made was that the application was suffering from a suspend/resume problem and that the opening of the file dialog was causing my application to suspend (and somehow break its voice commands) before choosing a file such that when my application resumed the voice commands were broken.
Why would my app suspend/resume just to display a file picker? I’d noticed previously that there is a file dialog process running on HoloLens so perhaps it’s fair to assume/guess that opening a file involves switching to another app altogether and, naturally, that might mean that my application suspends during that process.
I remember that this was also possible under phone implementations and (if I remember correctly) the separate-process model on phones was the reason why the UWP ended up with AndContinue() style APIs in the early days when the phone and PC platforms were being unified.
Taking that assumption further – it’s well known that when you are debugging a UWP app in Visual Studio the “Process Lifecycle Management” (PLM) events are disabled by the debugger. That’s covered in the docs here and so I could understand why my app might be working in the debugger and not working outside of the debugger.
That said, I did find that my app still worked when I manually used the debugger’s capability to suspend/resume (via the toolbar) which was a bit of a surprise as I expected it to break but I was fairly convinced by now that that my problem was due to suspend/resume.
So, it seems like I have a suspend/resume problem. What to do about it?
Resolving the Suspend/Resume Problem with Speech
My original code was using speech services provided by the Mixed Reality Toolkit’s SpeechInputSource.cs and SpeechInputHandler.cs utilities and I tried quite a few experiments around enabling/disabling these around suspend/resume events from the system but I didn’t find a recipe that made them work.
I took away my use of that part of the MRTK and started directly using SpeechRecognizer myself so that I had more control of the code & I kept that code as minimal as possible.
I still hit problems. My code was organised around spinning up a single SpeechRecognizer instance, keeping hold of it and repeatedly asking it via the RecognizeAsync() method to recognise voice commands.
I would find that this code would work fine until the process had suspended/resumed and then it would break. Specifically, the RecognizeAsync() code would return Status values of Success and Confidence values of Rejected.
So, it seemed that having a SpeechRecognizer kicking around across suspend/resume cycles wasn’t the best strategy and I moved to an implementation which takes the following approach;
- instantiate SpeechRecognizer
- add to its Constraints collection an instance of SpeechRecognitionListConstraint
- compile the constraints via CompileConstraintsAsync
- call RecognizeAsync making a note of the Text result if the API returns Success and confidence is Medium/High
- Dispose of the SpeechRecognizer and repeat regardless of whether RecognizeAsync returns a relevant value or not
and the key point seemed to be to avoid keeping a SpeechRecognizer instance around in memory and repeatedly calling RecognizeAsync on it expecting that it would continue to work across suspend/resume cycles.
I tried that out, it seems to work & I shipped it off into Store as a V3.0.
I have to admit that it didn’t feel like a very scientific approach to getting something to work – it was more trial and error so if someone has more detail here I’d welcome it but, for the moment, it’s what I settled on.
One last point…
Debugging this Scenario
One interesting part of trying to diagnose this problem was that I found the Unity debugger to be quite helpful.
I found that I could do a “script debugging” build from within Unity and then run that up on my device. I could then use my first speech command to open/cancel the file picker dialog before attaching Unity’s script debugger to that running instance in order to take a look around the C# code and see how opening/cancelling the file dialog had impacted my code that was trying to handle speech.
In some fashion, I felt like I was then debugging the app (via Unity) without really debugging the app (via Visual Studio). It could be a false impression but, ultimately, I think I got it working via this route
Amazing, thanks for sharing Mike. This was really really nice to read through.
Great post. Thanks for sharing this … some of the practical difficulties of developing on rapidly evolving platforms! What “should be” fairly straightforward is far from it!