控制台模板应用程序 MediaCapture 无法录制视频

Console template application MediaCapture fails to record video

我正在尝试在最小的基于 C# UWP 控制台模板的应用程序上使用 MediaCapture 来捕获视频。使用 InitializeAsync() 初始化 MediaCapture 有效,但实际开始录制失败,错误代码为 0xc00d3e82 / MF_E_INVALID_STATE_TRANSITION.

我已将 C# UWP 控制台应用程序模板安装到 Visual Studio 2017 以处理使用 MediaCapture 捕获视频的最小应用程序(在这种情况下不需要 GUI,因此不需要控制台应用程序)。最低目标是 Windows build 1803,因为这是 C# UWP 控制台应用程序所需的最低要求。

我已经尝试 运行需要使用 ConfigureAwait(false) 等待的方法,但这似乎没有任何区别。

关于功能,由于 UWP 控制台应用程序不会显示获取摄像头、麦克风等访问权限的权限提示,我在 运行 之前通过应用程序的设置手动授予它们。我相信所有必要的功能都包含在清单中,因为应用程序在某些情况下确实可以工作(请参阅代码块后的段落)。

最小的可重现示例,应该使用 C# UWP 控制台应用程序模板构建和运行:

using System;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Storage;

namespace MinimalMediaCaptureConsoleTest
{
    class Program
    {
        private static void MediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
        {
            Console.WriteLine("Media capture failed: error message: '" + errorEventArgs.Message + "', code: " + errorEventArgs.Code.ToString("X"));
        }
        static void Main(string[] args)
        {
            Task t = MainAsync(args);
            t.Wait();
            Task.Delay(2000).Wait(); // give time to see result before exiting
        }
        static async Task MainAsync(string[] args)
        {
            var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
            var cameraDevice = videoDevices[0];
            if (cameraDevice == null)
            {
                Console.WriteLine("No camera device found!");
                return;
            }

            MediaCapture mc = new MediaCapture();
            MediaCaptureInitializationSettings mcSettings = new MediaCaptureInitializationSettings
            {
                AudioDeviceId = "",
                VideoDeviceId = cameraDevice.Id,
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.VideoPreview
            };
            mc.Failed += MediaCapture_Failed;
            try
            {
                await mc.InitializeAsync(mcSettings);

            } catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("No access to the camera: " + e);
            }

            LowLagMediaRecording mediaRecording = null;
            var myVideos = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Videos);
            StorageFile file = await myVideos.SaveFolder.CreateFileAsync("mytestvid.mp4", CreationCollisionOption.GenerateUniqueName);
            mediaRecording = await mc.PrepareLowLagRecordToStorageFileAsync(
                      MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), file);

            await mediaRecording.StartAsync();
            Console.WriteLine("Started recording, press enter to stop");
            Console.ReadLine();
            await mediaRecording.StopAsync();
        }
    }
}

代码 运行 在从控制台应用程序的 Main() 启动的单独异步任务中,但是我也尝试使 Main 本身成为一个异步任务并 运行ning MediaCapture 代码直接从那里开始,行为没有区别。

有趣的是,如果我 运行 使用 Visual Studio 调试器的应用程序或在尝试录制视频之前将调试器附加到进程,视频捕获工作正常。但是,如果从命令提示符/Powershell 或开始菜单 运行 调用 LowLagMediaRecording 实例的 StartAsync() 方法将导致上述错误代码 0xc00d3e82 / MF_E_INVALID_STATE_TRANSITION 并且没有录制视频。

非常感谢 运行 在没有调试器的情况下会出现什么问题以及如何修复它。

将其作为 UWP 应用程序有什么特别的原因吗? (可能有某些 issues/complications 带有 UWP 控制台应用程序和后台录制) 如果 UWP 不重要,您也可以在 Win32 控制台应用程序中使用 MediaCapture,这在这种情况下似乎更适合(也是更简单的解决方案)- 对于 C#,请参考 - https://github.com/microsoft/WindowsVisionSkillsPreview/blob/master/samples/SentimentAnalyzerCustomSkill/cs/Apps/FaceSentimentAnalysisApp_.NETCore3.0/FaceSentimentAnalysisApp_.NETCore3.0.csproj

注意 - 此 C# 示例不仅仅 Windows MediaCapture。但是,您可以忽略其他内容,仅参考示例中的项目配置,并使用您目前拥有的相同 C# 代码。 您将需要 .Net Core 3.0 和 Visual Studio 2019 预览版才能使用它。
上述 .csproj 文件中的以下行很重要: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

<Reference Include="Windows">
  <HintPath>C:\Program Files (x86)\Windows Kits\UnionMetadata\Facade\Windows.WinMD</HintPath>
  <IsWinMDFile>true</IsWinMDFile>
</Reference>

<Reference Include="Windows.Foundation.FoundationContract">
  <HintPath>C:\Program Files (x86)\Windows Kits\References.0.17763.0\Windows.Foundation.FoundationContract.0.0.0\Windows.Foundation.FoundationContract.winmd</HintPath>
  <IsWinMDFile>true</IsWinMDFile>
</Reference>

<Reference Include="Windows.Foundation.UniversalApiContract">
  <HintPath>C:\Program Files (x86)\Windows Kits\References.0.17763.0\Windows.Foundation.UniversalApiContract.0.0.0\Windows.Foundation.UniversalApiContract.winmd</HintPath>
  <IsWinMDFile>true</IsWinMDFile>
</Reference>

或者对于 C++,请参考 - https://github.com/microsoft/Windows-Camera/tree/master/Samples/WMCConsole_winrtcpp