Playing sounds does not work on IoS when AudioStreamer is initialized
I've been working on my own voice recognition using plugin.maui,.audio and got that working on Android, IoS, and Windows.
I started to add in playback items like wake tones. This is when I noticed something odd. On Android, the sounds work and text to speech works great.
On IoS, there's nothing. However, if I pause the startup (setting a breakpoint), the audio does play at least the first time.
If I let my main program start up and run without breakpoints, I get an error
The operation couldn’t be completed. (OSStatus error 561017449.)
So I assume that has something to do with what is going on... It's something about resources are already allocated or being used (or something like that)
Loading a wav file like this:
playerIsAwake = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("isAwake.wav"));
Both sound out lines:
await TextToSpeech.Default.SpeakAsync(text, cancelToken: token).ConfigureAwait(false)
and
playerIsAwake.Play();
Work on Android and Windows after initializing and fail to play anything in IoS, unless I comment out the line:
await _audioStreamer.StartAsync();
If that line is commented out, then the IoS playback works, but of course the streamer is not initialized.
Since my program has a lot of things in it, I decided to set up a minimal code test in an attempt to reproduce the issue.
When I did this, the above error goes away, probably due to the class injection in Maui which I'm not doing in this test app. However, the issue still exists. Android and Windows work great, IoS does not. (Again, unless I comment out the above line)
To help out, I created a repository with the demo code. You'll only need to add in your bundle signing to get it to deploy on IoS. The repository is here: [https://github.com/sej69/AudioManagerTest/tree/master/AudioManagerTest](GitHub AudioManagerTest)
Can you try #197 and see if that works for you?
Happy to.
Since this is deployed through nuget and I need to install an updated version, do I copy this into my nuget folder? Or what is the best way to try this?
If you scroll down on this page: https://github.com/jfversluis/Plugin.Maui.Audio/actions/runs/19080681184?pr=197 You can download the NuGet it generated and install that locally. You can add a local folder as a NuGet feed in VS or your NuGet.config file.
I just tested it and it does allow me to play audio. I then brought it into my main project and it started playing the text to speech audio which got cut off at some point; not sure if it was due to the streamer starting or not? But then the streaming speech to text stopped working.
I then went back to v4.0.0 and the speech to text started working again, but of course the audio doesn't.
In further examination, it looks like something happened with audio stream / memory stream. There is data there, but it's no longer recognized with Whisper. (I had to add headers and blank space prior to making it work with 4.0.0)
Also, the error still exists with another error:
`[0:] MyApp Error: 0 : [0:] failed to set category
[0:] MyApp Error: 0 : [0:] The operation couldn’t be completed. (OSStatus error 561017449.)
[0:] Initial activation failed. Attempting to deactivate and reactivate.
[0:] Warning: Failed to deactivate existing audio session: Session deactivation failed`
But this only shows on the first sound being loaded, not all of them:
`playerIsAwake = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("isAwake.wav")); // error shows here
playerIsSleeping = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("isAsleep.wav")); // but not here and it used to show here, too`
I believe this is fixed. However, I did encounter an odd occurrence to note.
I updated the demo project to include the recording and to my joy, it worked.
I then went back to my app to see what the issue was.
The issue turned out that I wasn't able to play a wav file in a parent class. However, if I placed the wav file in the audio processing class then it worked. I can see in the debug log in my master project that the player does reach the play and it doesn't cause an exception; it just doesn't play.
The test project was flat, the classes were part of the project, not an additional DLL project.
So now as a workaround, I'm going to place all audio tasks in the AudioStreaming object which will work.
Please publish this update in a near future update.
Final update.
I thought you'd like this additional information.
I was having difficulties with getting this to work in my regular code. But I did have a working reference to work off of.
What I found is that even though I'm injecting the AudioManager into the builder and referencing the injected class, I would get those errors above and speech recognition would go offline as well as not being able to play sounds.
The errors I got were:
`[0:] MyApp Error: 0 : [0:] failed to set category
[0:] MyApp Error: 0 : [0:] The operation couldn’t be completed. (OSStatus error 561017449.)
I then tried to inject an audio player from the parent class and that still had the same issues (Again referencing the injected AudioManager)
I then moved all audio code into the event class which does the speech recognition and I got no errors and it works perfectly.
Then to test the code update you provided, I went to version 4 and it stopped working. I then went back to the one you compiled and sent out and its again working.
I'll watch for your next published update with this code in it.