Hello Guys,
My UWP app will call [MediaCapture] relevant APIs to take some photos. Now, if I switch different users and the start the app again, the code will catch exception when it run into the call StartPreviewAsync
method. The issue doesn't occur everytime, instead it occurs randomly.
My project target and min platform version both are 16299.
The app runs on the windows 10(Build 10.0.18362.1);
The code block looks like the following:
private async void MainPage_Loaded(object sender, RoutedEventArgs)
{
await this.InitCapture();
}
private async Task InitCapture()
{
var mediaCapture = new MediaCapture();
DeviceInformation camera = await this.GetCameraInfo();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Auto,
AudioDeviceId = string.Empty,
VideoDeviceId = camera.Id
});
capturePreview.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
var allProperties = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x=>new StreamResolution(x));
var streamResolutions = allProperties.Where(x=>("MJPG".Equals(x.EncodingProperties.Subtype) || "JPEG".Equals(
x.EncodingProperties.Subtype)) && x.Width >= x.Height).OrderByDescending(x=>x.FrameRate);
var streamResolution = streamResolutions.Where(x=> x.Height == (uint)1080 && x.Width == (uint)1920).FirstOrDefault();
if(streamResulotion != null)
{
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, this.streamResolution.EncodingProperties);
}
else
{
streamResolution = GetRatio(streamResolutions, 1080, 1920);
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, this.streamResolution.EncodingProperties);
}
}
private async Task<DeviceInformation> GetCameraInfo()
{
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation device = allVideoDevices.FirstOrDefault(x=>{return x.EnclosureLocation != null && x.EnclosureLocation.Panel == Panel.Back;
});
return device ?? allVideoDevices.FirstOrDefault();
}
private StreamResolution GetRatio(IEnumerable<StreamResolution> streamResolutions, uint height, uint width)
{
StreamResolution streamResolution = null;
double selectRatio = (double) width /height;
for(int i = streamResolutions.Count() - 1; i >= 0; i--)
{
streamResolution = streamResolutions.ElementAt(i);
if(Math.Abs(streamResolution.AspectRatio - selectRatio) < 0.1)
{
break;
}
}
if(streamResolution == null)
{
double dMin = 100.0d;
for(int i = streamResolutions.Count() - 1; i >= 0; i--)
{
StreamResolution resolutionTemp = streamResolutions.ElementAt(i);
double fDistance = Math.Abs(resolutionTemp.AspectRatio - selectRatio);
if(fDistance < dMin)
{
dMin = fDistance;
streamResolution = resolutionTemp;
}
}
}
return streamResolution;
}
<StackPanel Orientation="Horizontal">
<CaptureElement x:Name="capturePreview" Width="320" Height="240" />
</StackPanel>
[Updated 2023/03/24]
The exception message:
CLR.Exception.System.Exception._message
The media source cannot move from a stopped state to a temporarily stopped state. App in background
I collected the app dmp and got detailed stack information:
STACK_TEXT:
00000000`00000000 00007ffc`481dd931 SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+0x21
00000000`00000000 00007ffc`481dd840 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ThrowForNonSuccess+0x70
00000000`00000000 00007ffc`481dd7c8 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.HandleNonSuccessAndDebuggerNotification+0x38
00000000`00000000 00007ffc`481dd787 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ValidateEnd+0x17
00000000`00000000 00007ffc`4855fa0b SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.GetResult+0xb
00000000`00000000 00007ffc`3084c675 WinApp+0x219c675
00000000`00000000 00007ffc`481dd931 SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+0x21
00000000`00000000 00007ffc`481dd840 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ThrowForNonSuccess+0x70
00000000`00000000 00007ffc`481dd7c8 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.HandleNonSuccessAndDebuggerNotification+0x38
00000000`00000000 00007ffc`481dd787 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ValidateEnd+0x17
00000000`00000000 00007ffc`4855fa0b SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.GetResult+0xb
00000000`00000000 00007ffc`3084c445 WinApp+0x219c445
00000000`00000000 00007ffc`481dd931 SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+0x21
00000000`00000000 00007ffc`481dd840 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ThrowForNonSuccess+0x70
00000000`00000000 00007ffc`481dd7c8 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.HandleNonSuccessAndDebuggerNotification+0x38
00000000`00000000 00007ffc`481dd787 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ValidateEnd+0x17
00000000`00000000 00007ffc`4855fa0b SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.GetResult+0xb
00000000`00000000 00007ffc`3085ac2f WinApp+0x21aac2f
00000000`00000000 00007ffc`481dd931 SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+0x21
00000000`00000000 00007ffc`481dd840 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ThrowForNonSuccess+0x70
00000000`00000000 00007ffc`481dd7c8 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.HandleNonSuccessAndDebuggerNotification+0x38
00000000`00000000 00007ffc`481dd787 SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.ValidateEnd+0x17
00000000`00000000 00007ffc`4855fa0b SharedLibrary!System::Runtime::CompilerServices::TaskAwaiter.GetResult+0xb
00000000`00000000 00007ffc`3087f429 WinApp+0x21cf429
00000000`00000000 00007ffc`481dd931 SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+0x21
00000000`00000000 00007ffc`4829849e SharedLibrary!System::Runtime::CompilerServices::AsyncMethodBuilderCore::<>c.<ThrowAsync>b__7_0+0x1e
00000000`00000000 00007ffc`48089498 SharedLibrary!System::Threading::SendOrPostCallback.Invoke+0x28
00000000`00000000 00007ffc`482790c3 SharedLibrary!System::Threading::WinRTSynchronizationContext::Invoker.InvokeCore+0x33
FAULTING_SOURCE_LINE: f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs
FAULTING_SOURCE_FILE: f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs
FAULTING_SOURCE_LINE_NUMBER: 63
FAULTING_SOURCE_CODE:
No source found for 'f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs'
SYMBOL_NAME: SharedLibrary!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw+21
MODULE_NAME: SharedLibrary
IMAGE_NAME: SharedLibrary.dll
FAILURE_BUCKET_ID: STOWED_EXCEPTION_System.Exception_c00d3e82_SharedLibrary.dll!System::Runtime::ExceptionServices::ExceptionDispatchInfo.Throw
OS_VERSION: 10.0.18362.1
BUILDLAB_STR: 19h1_release
OSPLATFORM_TYPE: x64
OSNAME: Windows 10
IMAGE_VERSION: 2.2.27405.0