如何在 UWP 应用程序中播放来自 JS 的声音?
How to play a sound from JS in a UWP app?
我正在开发一个 UWP,其中包含一个 WebApp,该 WebApp 具有一些调用某些 C# 函数的 JS 函数。现在,我正在尝试播放存储在我的 UWP 应用的资产文件夹中的声音:
这是我正在尝试使用我的 Windows 运行时组件:
的功能
public async void Beep()
{
await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
MediaElement mysong = new MediaElement();
StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFolder soundsFolder = await folder.GetFolderAsync("sounds");
StorageFile file = await soundsFolder.GetFileAsync("beep.mp3");
var stream = await file.OpenAsync(FileAccessMode.Read);
mysong.SetSource(stream, file.ContentType);
mysong.Play();
});
}
顺便说一句,我也试过这个代码,但它也没有用:
public async void Beep()
{
MediaElement mysong = new MediaElement();
StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFolder soundsFolder = await folder.GetFolderAsync("sounds");
StorageFile file = await soundsFolder.GetFileAsync("beep.mp3");
var stream = await file.OpenAsync(FileAccessMode.Read);
mysong.SetSource(stream, file.ContentType);
mysong.Play();
}
这是文件位置的结构:
这就是我从 JS:
调用它的方式
CallJSCSharp.Beep();
我在 Windows 运行时组件中有更多功能,除了这个以外,所有功能都按预期工作。感谢您的帮助。
MediaElement
需要添加到 XAML UI 树才能工作;你应该只使用 MediaPlayer
API instead. You don't even need to write C# code to do it - you should be able to access the API directly from JavaScript (if you want to). More on the Media Playback overview page
我找到了修复方法:
我在同一个解决方案中添加了两个项目:
- UWP 项目。
- Windows 运行时组件。
我从函数中删除了额外的代码(没有必要按照之前的建议在 XAML 中添加媒体元素)。
public async void Beep()
{
MediaElement mysong = new MediaElement();
StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFolder soundsFolder = await folder.GetFolderAsync("sounds");
StorageFile file = await soundsFolder.GetFileAsync("beep.mp3");
var stream = await file.OpenAsync(FileAccessMode.Read);
mysong.SetSource(stream, file.ContentType);
mysong.Play();
}
重要的是要记住当你调用任何 JS 函数时,它必须使用 小写字母 因为 JS 约定(即使你的函数已经用大写字母编写).
CallJSCSharp.beep();
有关 JS 约定的更多信息:
我正在开发一个 UWP,其中包含一个 WebApp,该 WebApp 具有一些调用某些 C# 函数的 JS 函数。现在,我正在尝试播放存储在我的 UWP 应用的资产文件夹中的声音:
这是我正在尝试使用我的 Windows 运行时组件:
的功能public async void Beep()
{
await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
MediaElement mysong = new MediaElement();
StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFolder soundsFolder = await folder.GetFolderAsync("sounds");
StorageFile file = await soundsFolder.GetFileAsync("beep.mp3");
var stream = await file.OpenAsync(FileAccessMode.Read);
mysong.SetSource(stream, file.ContentType);
mysong.Play();
});
}
顺便说一句,我也试过这个代码,但它也没有用:
public async void Beep()
{
MediaElement mysong = new MediaElement();
StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFolder soundsFolder = await folder.GetFolderAsync("sounds");
StorageFile file = await soundsFolder.GetFileAsync("beep.mp3");
var stream = await file.OpenAsync(FileAccessMode.Read);
mysong.SetSource(stream, file.ContentType);
mysong.Play();
}
这是文件位置的结构:
这就是我从 JS:
调用它的方式CallJSCSharp.Beep();
我在 Windows 运行时组件中有更多功能,除了这个以外,所有功能都按预期工作。感谢您的帮助。
MediaElement
需要添加到 XAML UI 树才能工作;你应该只使用 MediaPlayer
API instead. You don't even need to write C# code to do it - you should be able to access the API directly from JavaScript (if you want to). More on the Media Playback overview page
我找到了修复方法:
我在同一个解决方案中添加了两个项目:
- UWP 项目。
- Windows 运行时组件。
我从函数中删除了额外的代码(没有必要按照之前的建议在 XAML 中添加媒体元素)。
public async void Beep() { MediaElement mysong = new MediaElement(); StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); StorageFolder soundsFolder = await folder.GetFolderAsync("sounds"); StorageFile file = await soundsFolder.GetFileAsync("beep.mp3"); var stream = await file.OpenAsync(FileAccessMode.Read); mysong.SetSource(stream, file.ContentType); mysong.Play(); }
重要的是要记住当你调用任何 JS 函数时,它必须使用 小写字母 因为 JS 约定(即使你的函数已经用大写字母编写).
CallJSCSharp.beep();
有关 JS 约定的更多信息: