I’m having a problem in multiple versions of Unity (2019.4.24f1, 2020.3.4f1) that started in the last couple of weeks with a project that hasn’t really changed. It feels like either a Unity or Oculus update causes an issue?
The problem: on Oculus Quest, the first time I run either build/run from Unity or installing and then launching on the headset, audio seems fine in an intro scene, then I load into my multiplayer scene, I get a microphone failed message, and all audio no longer plays in the level (and obviously the mic doesn’t work.) When I quit and launch again from the headset, all works correctly. (It has also happened on a few rare occasions that wasn’t the first launch.)
I’ve made sure I have RECORD_AUDIO permission in my custom AndroidManifest (and checked the generated manifest in Temp/StagingArea and confirmed it survived the merge), and I’ve called Permission.HasUserAuthorizedPermission(Permission.Microphone) which returns true.
Despite that, when the failure happens, I get this in logcat:
ServiceUtilities: Request denied by app op: 27
AudioPolicyIntefaceImpl: getInputForAttr permission denied: recording not allowed for uid 10117 pid 7709
AudioFlinger: createRecord() getInputForAttr return error -1 I
AudioFlinger: createRecord returned error -1
AudioRecord: createRecord_l(4): AudioFlinger could not create record track, status: -1
libOpenSLES: android_audioRecorder_realize(0x74663a6000) error creating AudioRecord object; status -1
libOpenSLES: Leaving Object::Realize (SL_RESULT_CONTENT_UNSUPPORTED) Unity : Starting microphone failed: "An error occured that wasn't supposed to. Contact support. " (33)
Unity : Dissonance.Audio.Capture.BasicMicrophoneCapture:StartCapture(String)
Unity : Dissonance.Audio.Capture.CapturePipelineManager:RestartTransmissionPipeline(String)
Unity : Dissonance.Audio.Capture.CapturePipelineManager:Update(Boolean, Single)
Unity : Dissonance.DissonanceComms:Update()
As I said, if I then exit the app and just relaunch from the headset, it usually works. I’ve tried everything I can think of to work around it - changing Android SDK Min/Target versions, tweaks to manifest, etc
The logcat output seems to indicate that the app does not have permission even though HasUserAuthorizedPermission returned true.
Update: if I insert a test scene before my main scene, I can successfully open the mic and record, then continue to loading this 2nd level, and the same error above will happen.
More importantly, I can disable Dissonance completely and all audio in the scene will still fail to play, and there are later some logcat lines:
Sorry for the delay. I did submit a bug, and they wrote requesting a reproducible case - which, as usual, is challenging - I spent a long time digging into it and I think I finally narrowed in on what is causing the issue: audio focus.
If feels like something has changed in the last month or so (Quest update?) that is changing the behavior of the system.
Here’s what appears to be happening: I have a native plugin that spins off an Android service to handle some networking and other tasks. This service will get spun off if it currently isn’t running, which is why the first time I would run my app after a reboot or installing an update, I’d get the audio issue - and subsequent runs would be ok. The service doesn’t do any audio, but the Unity process appears to lose Android audio focus just by starting up this service. (Which has worked fine for a couple of years now, which is why I didn’t suspect it earlier.) So that’s why audio works when the app is first launched, and then when my native plugin runs at some point and spins off this service, Unity is silent from then on and mic requests will fail. It isn’t really an error, so there was nothing standing out in the endless sea of logcat output.
So I guess the question is: can Unity detect it has lost Android audio focus, and what can my app do about it?
I’ve been working on capturing a wav for wit.ai and noticed that the recent “Hey Facebook” voice command addition may have broken something with the mic process
I tried this. It did not succeed in re-activating the microphone or audio. However I called AudioSettings.Mobile.audioOutputStarted and it returns true rather confusingly.