{"record":{"id":"7b14879124cda51e","repo":"stride3d/stride","slug":"failed-to-create-an-audiolayer-source-7b1487","errorCode":null,"errorMessage":"Failed to create an AudioLayer Source","messagePattern":"Failed to create an AudioLayer Source","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Audio/SoundInstanceStreamedBuffer.cs","lineNumber":49,"sourceCode":"        {\n            this.scheduler = scheduler;\n            Listener = listener;\n            engine = soundStreamedBuffer.AudioEngine;\n            sound = soundStreamedBuffer;\n            spatialized = soundStreamedBuffer.Spatialized;\n\n            if (engine.State == AudioEngineState.Invalidated)\n                return;\n\n            //We first create the soundSource so that it gets initialized and can give us sampleRate and Channels info\n            soundSource = streamedSource = new StreamedBufferSoundSource(this, this.scheduler, mediaDataUrl, startPosition, length);\n\n            //Create the AudioLayer source\n            Source = AudioLayer.SourceCreate(listener.Listener, soundStreamedBuffer.SampleRate, streamedSource.MaxNumberOfBuffers, \n                soundStreamedBuffer.Channels == 1, spatialized, true, useHrtf, directionalFactor, environment);\n\n            if (Source.Ptr == IntPtr.Zero)\n                throw new Exception(\"Failed to create an AudioLayer Source\");\n\n            ResetStateToDefault();\n        }\n\n        public void Seek(TimeSpan mediaTime)\n        {\n            streamedSource.Seek(mediaTime);\n        }\n\n        public bool ReachedEndOfMedia()\n        {\n            return streamedSource.ReachedEndOfMedia();\n        }\n\n        public bool SeekRequestCompleted()\n        {\n            return streamedSource.SeekRequestCompleted();\n        }","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Audio/SoundInstanceStreamedBuffer.cs#L31-L67","documentation":"SoundInstanceStreamedBuffer creates a native AudioLayer source for a streamed sound buffer; when AudioLayer.SourceCreate returns IntPtr.Zero the source could not be allocated and playback is impossible, so the constructor throws. The cause is almost always an unusable or overloaded audio device.","triggerScenarios":"Creating a SoundInstanceStreamedBuffer (streamed media playback, e.g. through SoundInstance/ media system) when the backend returns a zero source pointer: missing audio device, too many live sources, or unsupported spatialized/HRTF/directional configuration.","commonSituations":"Streaming music/video on devices without an active audio output, VMs or remote desktops without audio redirection, mobile apps exceeding the platform source cap with many simultaneous streams, requesting spatialized HRTF playback on unsupported backends.","solutions":["Ensure an audio output device is present and the AudioEngine is healthy; reinitialize the engine after device changes.","Limit concurrent streamed buffers and dispose completed instances to free native sources.","Retry with spatialized: false and useHrtf: false to rule out unsupported features.","Guard creation with try-catch and provide a silent fallback."],"exampleFix":"// before\nvar streamed = new SoundInstanceStreamedBuffer(listener, soundStreamedBuffer, spatialized: true, useHrtf: true);\n// after\nSoundInstanceStreamedBuffer streamed = null;\ntry { streamed = new SoundInstanceStreamedBuffer(listener, soundStreamedBuffer, spatialized: false, useHrtf: false); }\ncatch (Exception) { streamed = null; /* audio unavailable */ }","handlingStrategy":"try-catch","validationCode":"if (audioEngine.State == AudioEngineState.Invalidated)\n    return null; // skip streamed playback\nif (liveStreamedBuffers >= MaxConcurrentStreams)\n    return null;","typeGuard":"bool CanStreamAudio(AudioEngine engine) => engine != null && engine.State == AudioEngineState.Initialized;","tryCatchPattern":"SoundInstanceStreamedBuffer buffer = null;\ntry\n{\n    buffer = new SoundInstanceStreamedBuffer(listener, soundStreamedBuffer, spatialized: false, useHrtf: false);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"AudioLayer Source\"))\n{\n    Log.Warning(\"Streamed audio source allocation failed: {0}\", ex.Message);\n}","preventionTips":["Cap the number of simultaneous streamed buffers and release them deterministically.","Validate engine health before creating streamed media players.","Prefer non-spatialized playback for background music to minimize backend requirements.","Handle device hot-unplug by recreating the engine and its instances."],"tags":["audio","native-interop","streaming","device"],"backgroundTag":"module-init-failed","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}