3

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

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

我已经尝试运行需要 await with 的方法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 本身成为异步任务并直接从那里运行 MediaCapture 代码,行为没有区别。

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

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

4

1 回答 1

1

有什么特别的理由让它成为 UWP 应用程序吗?(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\10\UnionMetadata\Facade\Windows.WinMD</HintPath>
  <IsWinMDFile>true</IsWinMDFile>
</Reference>

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

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

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

于 2019-06-04T00:27:30.023 回答