如何在UWP应用程序中播放JS的声音?

编程入门 行业动态 更新时间:2024-10-11 01:11:26
本文介绍了如何在UWP应用程序中播放JS的声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个包含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的声音?

    本文发布于:2023-11-12 13:23:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1581628.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   声音   如何在   UWP   JS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!