Recover AudioStream when system audio output device changes#274
Open
MaxHeimbrock wants to merge 2 commits intomainfrom
Open
Recover AudioStream when system audio output device changes#274MaxHeimbrock wants to merge 2 commits intomainfrom
MaxHeimbrock wants to merge 2 commits intomainfrom
Conversation
Unity stops every AudioSource during the audio engine reset that follows a system output device change (per AudioSettings.OnAudioConfigurationChanged docs). Without re-playing the source, OnAudioFilterRead stops firing and the AudioStream goes silent until recreated. Subscribe to the event, re-Play() the source, and clear the ring buffer so we don't drain stale frames accumulated during the device-change blackout. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Calling _audioSource.Play() alone isn't enough to recover after Unity tears down the audio engine: the OnAudioFilterRead node from the existing AudioProbe stays detached from the rebuilt audio graph and no callbacks fire. Destroy the old probe, AddComponent a fresh one, and Stop()/Play() the source so Unity re-registers the filter node. Also drop the deviceWasChanged gate (programmatic Reset() needs the same recovery) and switch to a visible Debug.Log so the event firing can be confirmed in the Console. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
17e73ed to
9e4af91
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AudioStreamgoing silent on macOS / Android after the system audio output device changes (e.g. unplugging headphones), where the only previous workaround was disconnecting and re-subscribing the participant.AudioSettings.OnAudioConfigurationChangedand, in the handler, destroys the existingAudioProbe+AddComponents a fresh one andStop()/Play()s theAudioSource. The ring buffer is cleared and re-primed so we don't drain stale frames buffered during the device-change blackout.Why re-
Play()alone isn't enoughUnity tears down its native audio engine on device changes (macOS / AAudio).
AudioSources are stopped, but more importantly theOnAudioFilterReadregistration ofAudioProbelives on the DSP graph and is detached during the rebuild. Unity only re-registers filter components added after the rebuild — so we have to recreate theAudioProbeto get the filter slot back. iOS doesn't hit this because route changes there don't rebuild the engine.Test plan
MediaStreamLifetimeTestsupdated for the new unsubscribe).🤖 Generated with Claude Code