如何在UWP应用程序中播放JS的声音? [英] How to play a sound from JS in a UWP app?

查看:160
本文介绍了如何在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();
}

这是文件位置的结构:




  1. 我从函数中删除了额外的代码(它是不必要的按照之前的建议在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();
    }


  2. 重要的是要记住当你调用任何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:

  1. I added both projects in the same solution:

    • UWP project.
    • Windows Runtime Component.

  1. 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();
    }
    

  2. 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的声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆