You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Kinect SDK的Windows通用应用:XAML+C++播放本地视频遇阻

Hey Carlos, let's work through this local video playback issue in your Kinect-based UWP app. I’ve dealt with similar setups before, so here are some targeted steps to get your MediaPlayerElement working properly:

1. First, Verify UWP File Access Capabilities

UWP apps run in a sandbox, so you need to make sure your app has the right permissions to access local video files:

  • If you’re using a FileOpenPicker to let users select videos, you don’t need extra manifest permissions—the picker grants temporary access automatically.
  • If you’re accessing files from specific libraries (like Pictures or Videos), add the corresponding capability to your Package.appxmanifest:
    <Capabilities>
        <Capability Name="picturesLibrary" />
        <Capability Name="videosLibrary" />
    </Capabilities>
    
  • For arbitrary local folders, you’ll need to use the broadFileSystemAccess capability (note: this requires additional approval for Microsoft Store submission).
2. Properly Initialize the MediaPlayerElement in C++

Here’s how to set up your MediaPlayerElement with local video files, depending on where the video is stored:

Case 1: User-selected file via FileOpenPicker

Add this code to your page’s loaded event or initialization logic:

#include <winrt/Windows.Storage.Pickers.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Media.Core.h>
#include <winrt/Windows.Media.Playback.h>

winrt::fire_and_forget LoadUserSelectedVideo()
{
    auto picker = winrt::Windows::Storage::Pickers::FileOpenPicker();
    picker.SuggestedStartLocation(winrt::Windows::Storage::Pickers::PickerLocationId::VideosLibrary);
    picker.FileTypeFilter().Append(L".mp4");
    picker.FileTypeFilter().Append(L".avi");
    picker.FileTypeFilter().Append(L".mov");
    picker.FileTypeFilter().Append(L".wmv");

    auto selectedFile = co_await picker.PickSingleFileAsync();
    if (selectedFile)
    {
        auto mediaSource = winrt::Windows::Media::Core::MediaSource::CreateFromStorageFile(selectedFile);
        _MediaPlayerElement.MediaPlayer().Source(mediaSource);
        _MediaPlayerElement.MediaPlayer().Play();
    }
}

Case 2: Video included in your app package

If you’ve added a video to your project (mark it as Content in the file properties), load it like this:

winrt::fire_and_forget LoadPackageVideo()
{
    try
    {
        auto videoFile = co_await winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(
            winrt::Windows::Foundation::Uri(L"ms-appx:///Assets/YourVideoFile.mp4"));
        
        auto mediaSource = winrt::Windows::Media::Core::MediaSource::CreateFromStorageFile(videoFile);
        _MediaPlayerElement.MediaPlayer().Source(mediaSource);
    }
    catch (const winrt::hresult_error& ex)
    {
        // Handle file not found or loading errors
        auto errorMsg = ex.message();
    }
}
3. Troubleshoot Common Playback Failures
  • Check video encoding: UWP’s MediaPlayer supports H.264, MPEG-4, WMV, and some other formats, but it won’t play unsupported codes (like VP9 without extra extensions). Use a tool like MediaInfo to confirm your video’s codec is compatible.
  • Listen for error events: Attach a MediaFailed handler to get detailed error info when playback fails:
    _MediaPlayerElement.MediaPlayer().MediaFailed([this](auto&&, const winrt::Windows::Media::Playback::MediaPlayerFailedEventArgs& args)
    {
        auto errorMessage = args.ErrorMessage();
        // Log this message to debug the root cause
    });
    
  • Avoid UI thread blocking: Always use co_await for async file operations—blocking the UI thread can prevent the MediaPlayerElement from rendering properly.
4. Rule Out Kinect SDK Conflicts

Since your app is built on the CameraFrames Kinect SDK sample, there might be resource or pipeline conflicts between Kinect’s video capture and the MediaPlayer:

  • Temporarily disable Kinect initialization code and test video playback. If it works, the issue is likely a conflict.
  • Ensure Kinect’s video processing and MediaPlayer operations are running on separate dispatchers or background threads to avoid resource contention.

Let me know if any of these steps resolve your issue, or if you can share more details about the specific error you’re seeing!

内容的提问来源于stack exchange,提问作者Carlos Hernandez Perez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:13:06