How to access camera preview frames with MediaCapture class in Unity?

Valentin 40 Reputation points
2024-11-15T08:24:11.62+00:00

Using the Windows MediaCapture class we can capture videos and images on the HoloLens 2. The class also offers preview capabilities (see here). However, I am not sure how to get the preview stream within Unity and then display it to the user. In the docs they suggest to connect a XML CaptureElement to the mediaCapture object. What's the equivalent for use in Unity/on Hololens 2? A similar question without an answer has been asked 7 years ago here

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
HoloLens Development
HoloLens Development
HoloLens: A family of Microsoft self-contained, holographic devices that enable engagement with digital content and interaction with holograms in the surrounding environment.Development: The process of researching, productizing, and refining new or existing technologies.
402 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ying Li - MSFT 1,285 Reputation points Microsoft Vendor
    2024-11-18T09:37:58.8433333+00:00

    Hello, Welcome to Microsoft Q&A,

    You can use the WinRT API to capture frame data, convert it to a Unity-compatible format, and then apply it to a Texture2D.

    Alternatively, you can also implement camera preview function using PhotoCaptureFrame or WebCamTexture provided by Unity, without using the WinRT API.

    If you use the WinRT API with Unity for HoloLens, ensure that the preprocessor directive ENABLE_WINMD_SUPPORT is wrapped around any WinRT-leveraged code. Note that this directive won't work in Unity Play mode. For more details, see - WinRT APIs with Unity for HoloLens - Mixed Reality | Microsoft Learn.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.