MicrosoftDocs / winrt-api

WinRT reference content for developing Microsoft Universal Windows Platform (UWP) apps
Creative Commons Attribution 4.0 International
230 stars 494 forks source link

PrepareLowLagRecordToStorageFileAsync randomly throws COMException #2357

Closed karmelcorn closed 1 year ago

karmelcorn commented 1 year ago

I have code to record multiple videos.

public async Task StartRecording(string fileName)
    {
        try
        {
            _tempVideoName = fileName ?? (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString() + ".mp4");

            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(_directory);
            StorageFile file = await folder.CreateFileAsync(_tempVideoName, CreationCollisionOption.ReplaceExisting);
            _mediaRecording = await _mediaCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), file);
            await _mediaRecording.StartAsync();
        }
        catch (Exception ex)
        {
            _logger.LogCritical(ex, "Start Recording error");
        }
    }

_mediaRecording = await _mediaCapture.PrepareLowLagRecordToStorageFileAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), file); will randomly throw the following exception

Exception thrown: 'System.Runtime.InteropServices.COMException' in WinRT.Runtime.dll
The object invoked has disconnected from its clients. (0x80010108 (RPC_E_DISCONNECTED))

and return a null object to_mediaRecording, causing the saving of the video file to fail.

Seems like the error occurs more often after recording of a larger file, around more than 25s. Multiple recording of videos below 20s does not cause the error.

Lucasty7 commented 1 year ago

Open

Lucasty7 commented 1 year ago

https://instagram.com/adela.rutz?igshid=YmMyMTA2M2Y=

Lucasty7 commented 1 year ago

_mediaRecording = await

karmelcorn commented 1 year ago

Resolved the issue by ensuring that MediaCapture was initialized on the main UI Thread.