{"record":{"id":"32553880ba829c67","repo":"stride3d/stride","slug":"videoinstance-mediacodec-failed-to-get-the-audioengine","errorCode":null,"errorMessage":"VideoInstance mediaCodec failed to get the AudioEngine","messagePattern":"VideoInstance mediaCodec failed to get the AudioEngine","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/Backends/MediaCodecVideoBackend.cs","lineNumber":75,"sourceCode":"        imageReader = ImageReader.NewInstance(videoWidth, videoHeight, ImageFormatType.Yuv420888, maxImages: 8);\n\n        rgbaScratchSize = videoWidth * videoHeight * 4;\n        rgbaScratch = Marshal.AllocHGlobal(rgbaScratchSize);\n\n        mediaSynchronizer = new MediaSynchronizer();\n\n        mediaCodecVideoExtractor = new MediaCodecVideoExtractor(Instance, mediaSynchronizer, imageReader.Surface);\n        mediaCodecVideoExtractor.Initialize(Instance.Services, url, startPosition, length);\n        mediaSynchronizer.RegisterExtractor(mediaCodecVideoExtractor);\n        mediaSynchronizer.RegisterPlayer(mediaCodecVideoExtractor);\n\n        Instance.SetDuration(mediaCodecVideoExtractor.MediaDuration);\n\n        var videoComponent = Instance.VideoComponent;\n        if (mediaCodecVideoExtractor.HasAudioTrack && videoComponent.PlayAudio)\n        {\n            var audioEngine = Instance.Services.GetService<IAudioEngineProvider>()?.AudioEngine\n                ?? throw new Exception(\"VideoInstance mediaCodec failed to get the AudioEngine\");\n\n            var isSpatialized = videoComponent.AudioEmitters.Any(x => x != null);\n            audioSound = new StreamedBufferSound(audioEngine, mediaSynchronizer, url, startPosition, length, isSpatialized);\n            mediaSynchronizer.RegisterExtractor(audioSound);\n\n            if (isSpatialized)\n            {\n                if (audioSound.GetCountChannels() == 1)\n                {\n                    foreach (var emitter in videoComponent.AudioEmitters)\n                    {\n                        if (emitter == null)\n                            continue;\n\n                        var controller = emitter.AttachSound(audioSound);\n                        mediaSynchronizer.RegisterPlayer(controller);\n                        audioControllers.Add(controller);\n                    }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/Backends/MediaCodecVideoBackend.cs#L57-L93","documentation":"During MediaCodecVideoBackend.Initialize, when the video has an audio track and PlayAudio is enabled, the backend could not obtain an AudioEngine from IAudioEngineProvider in Instance.Services, so it cannot create the StreamedBufferSound for the audio stream.","triggerScenarios":"IAudioEngineProvider missing from Instance.Services (GetService returned null); the audio engine is null because the audio subsystem was never initialized; video with audio track initialized before audio system startup.","commonSituations":"Custom/minimal service registries without audio; initialization ordering where video starts before Stride audio; platforms with audio support stripped while the media still contains an audio track.","solutions":["Register IAudioEngineProvider in the services and ensure the engine is constructed before video initialization","Initialize the Stride audio system before VideoComponent playback","Set VideoComponent.PlayAudio = false if audio is not needed (path is skipped)","Fix startup order so audio setup precedes video Initialize"],"exampleFix":"// before\n// no audio registration; video init throws\n// after\nservices.AddAudio(); // registers IAudioEngineProvider\nvar backend = new MediaCodecVideoBackend(); // then Initialize","handlingStrategy":"type-guard","validationCode":"var provider = Instance.Services.GetService<IAudioEngineProvider>();\nbool audioReady = provider?.AudioEngine != null;\nif (!audioReady) Instance.VideoComponent.PlayAudio = false; // or register audio first","typeGuard":"bool AudioAvailable(SceneInstance s) => s.Services.GetService<IAudioEngineProvider>()?.AudioEngine != null;","tryCatchPattern":"try { backend.Initialize(url, start, len); }\ncatch (Exception ex) when (ex.Message.Contains(\"failed to get the AudioEngine\"))\n{ /* init audio subsystem or disable PlayAudio, then retry */ }","preventionTips":["Initialize Stride audio before video backends","Register IAudioEngineProvider in startup","Set PlayAudio=false when audio isn't required"],"tags":["android","audio","audioengine","dependency"],"backgroundTag":"missing-dependency","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"}