基于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:
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
FileOpenPickerto 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
broadFileSystemAccesscapability (note: this requires additional approval for Microsoft Store submission).
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(); } }
- Check video encoding: UWP’s
MediaPlayersupports 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
MediaFailedhandler 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_awaitfor async file operations—blocking the UI thread can prevent theMediaPlayerElementfrom rendering properly.
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
MediaPlayeroperations 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

