我正在开发一个包含WebApp的UWP,该WebApp具有一些调用某些C#函数的JS函数。现在,我正在尝试播放我存储在我的UWP应用程序的Assets文件夹中的声音:
这是我正在尝试播放的函数我的 Windows运行时组件:
public async void Beep() {等待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(); }这是文件位置的结构:
我从函数中删除了额外的代码(它是不必要的按照之前的建议在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约定的更多信息:
在JavaScript中使用Windows运行时
I'm developing a UWP that contains a WebApp that has some JS functions that invokes some C# functions. Nowadays, I'm trying to play a sound that I have stored in the Assets folder of my UWP app:
This is the function that I'm trying to play of my Windows Runtime Component:
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(); }); }By the way, I also tried this code and it didn't work too:
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(); }This is structure of the location of the file:
And this is how I'm calling it from JS:
CallJSCSharp.Beep();I have more functions inside the Windows Runtime Component and all of them are working as expected with exception of this one. Thanks for your help.
解决方案I found the how to fix it:
I added both projects in the same solution:
- UWP project.
- Windows Runtime Component.
I removed extra code from the function (it's unnecessary to add the Media Element in the XAML as suggested before).
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(); }Important to remember when you call any JS function, it must be with lower cases because JS conventions (even if your function has been written with upper cases).
CallJSCSharp.beep();More information about JS Conventions:
Using the Windows Runtime in JavaScript
更多推荐
如何在UWP应用程序中播放JS的声音?
发布评论