如何在含WebApp的UWP应用中通过JS调用C#播放Assets音频?
Hey there! Let's tackle this audio playback issue you're having in your UWP + WebApp setup. Since your other Windows Runtime Component functions work perfectly, the problem is likely tied to how you're accessing the Assets folder audio file or handling the playback logic in C#. Here are some targeted fixes to try out:
First things first: make sure your audio file in the Assets folder has the correct build settings. Right-click the file in Visual Studio → Properties, then set:
- Build Action to
Content - Copy to Output Directory to
Copy if newer(orCopy alwaysfor testing)
If this is misconfigured, the file won't be accessible at runtime—even if the path looks right.
You can't use a simple relative path to access files in the Assets folder from your Runtime Component. Instead, use the app's installed location to build the correct file path. Here's how to do it:
using Windows.Storage; public async Task GetAudioFileAsync() { try { // Replace "your-audio-file.mp3" with your actual filename StorageFile audioFile = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\your-audio-file.mp3"); // Pass this file to your playback logic } catch (Exception ex) { // Log this to debug—file not found errors show up here System.Diagnostics.Debug.WriteLine($"File access error: {ex.Message}"); } }
Pro tip: Filenames are case-sensitive in UWP, so make sure the path matches exactly what's in your project.
If your playback code is missing key initialization steps, it'll fail silently. Here's a reliable MediaPlayer implementation that works well in Runtime Components:
using Windows.Media.Playback; using Windows.Storage; public async Task PlayAssetAudioAsync() { try { StorageFile audioFile = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\test-audio.mp3"); MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.Source = MediaSource.CreateFromStorageFile(audioFile); mediaPlayer.Play(); } catch (Exception ex) { // Capture errors here—this will tell you if the format is unsupported, etc. System.Diagnostics.Debug.WriteLine($"Playback failed: {ex.Message}"); } }
Adding error logging is critical here—check the Visual Studio Output window for exceptions, they'll point directly to the root cause.
Even if other functions work, make sure your JavaScript is awaiting the async playback function (if it's marked async). A common silent failure is forgetting to await an async C# method:
// Make sure to await the async playback function await YourRuntimeComponent.PlayAssetAudioAsync();
If you skip the await, the function might exit before playback can initialize.
While local audio playback usually doesn't need extra permissions, double-check your UWP app manifest to ensure there's no restrictive settings blocking audio. For basic playback, you don't need special capabilities, but if you're testing background playback, enable the Background Media Playback capability.
If you try these steps and still hit issues, share the exact code of your playback function and any error messages from the debug output—that'll help narrow down the problem even further!
内容的提问来源于stack exchange,提问作者Federico Navarrete

