UWP 自定义 Toast 通知声音无法在移动设备上播放 [英] UWP custom toast notification sound doesn't play on mobile

查看:59
本文介绍了UWP 自定义 Toast 通知声音无法在移动设备上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个 xml 作为通知正文,其中包含带有指向预设 Windows 声音的源 (src) 属性的音频元素,它不播放我想要的声音,而是播放默认的系统声音.我的通知 xml 看起来像这样(我将其用作测试消息以通过 Azure 通知中心调试选项发送)

So I have an xml as notification body witch includes audio element with source (src) attribute pointing to preset windows sound and it doesn't play the sound I want and instead plays the default system sound. My notification xml looks like this (I use this as a test message to send trough Azure notification hubs debug option)

<?xml version="1.0" encoding="utf-8"?> 
<toast> 
    <visual>
        <binding template="ToastText01">
            <text id="1">Test message</text>
        </binding>
    </visual>
    <audio src="ms-winsoundevent:Notification.Looping.Alarm" loop="false"/>
</toast>

我的应用程序没有任何 Toast 处理(没有启动后台任务或任何事情).有趣的是,我的电脑在收到通知时会播放它应该播放的声音,但手机每次都播放默认声音.

I don't have any toast handling on my app (no background task is launched or anything). Funny thing is that my PC plays the sound it should when it recieves the notification, but phone plays default sound every time.

我至少需要播放预设的 Windows 声音,但是从本地文件播放自定义声音会很好(这也不适用于自定义声音).另外,如果您知道是否有可能从 Toast 通知触发的后台任务开始播放音乐,请告诉我,我在 google 上找不到关于此问题的任何信息.

I need to at least play preset windows sound, but playing custom sound from local files would be ace (this doesn't work with custom sounds neither). Also if you know if there's a possibility to start playing music from background task triggered by toast notification let me know, I couldn't find any info on google on this matter.

这是告诉我的 xml 是好的微软链接(即使它不起作用):https://msdn.microsoft.com/en-us/library/windows/apps/br230842.aspx

This is the microsoft link that tells my xml is good (even though it doesn't work): https://msdn.microsoft.com/en-us/library/windows/apps/br230842.aspx

推荐答案

我的应用程序没有任何 Toast 处理(没有启动后台任务或任何事情).有趣的是,我的电脑在收到通知时会播放它应该播放的声音,但手机每次都播放默认声音.

I don't have any toast handling on my app (no background task is launched or anything). Funny thing is that my PC plays the sound it should when it recieves the notification, but phone plays default sound every time.

看起来前缀为 ms-winsoundevent:Notification.Looping 的所有值都将被系统声音替换,同时将循环元素设置为 false.根据我的理解,这应该是预期的结果,这些值是为 Looping audio 设置的,如果您需要禁用 Looping,请使用前 5 个值,例如:ms-winsoundevent:Notification.IM

Looks like all values which prefix is ms-winsoundevent:Notification.Looping will be replaced by system sound while set loop element to false. Based on my understanding, this should be an expected result, these values are for Looping audio, if you need to disable looping, use the first 5 values, for example: ms-winsoundevent:Notification.IM

但是从本地文件播放自定义声音会很好(这也不适用于自定义声音)

but playing custom sound from local files would be ace (this doesn't work with custom sounds neither)

这是一个已知问题,在此 文章

This is a known issue which was mentioned in this article

原因是路径解析器有问题解决ms-appx:///path,所以音频src将被视为无效,然后默认声音将播放.

The reason is path parser has an issue to resolve ms-appx:/// path, so the audio src will be regarded as Invalid, then the default sound will be played.

解决方法是以编程方式将您的 wav 音频文件复制到 LocalFolder 并使用 "ms-appdata:///local/" 协议,例如:

The workaround is copying your wav audio file programmatically to LocalFolder and using the "ms-appdata:///local/" protocol, for example:

private async void Button_Click(object sender, RoutedEventArgs e)
{
            Windows.Storage.StorageFile audioFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/sound.wav"));
            Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            await audioFile.CopyAsync(localFolder);

            AddNotification();
}

public void AddNotification()
{
            ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

            XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
            toastTextElements[0].AppendChild(toastXml.CreateTextNode("This is a Toast Message"));

            IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
            ((XmlElement)toastNode).SetAttribute("launch", "MainPage.xaml");

            XmlElement audio = toastXml.CreateElement("audio");
            audio.SetAttribute("src", "ms-appdata:///local/sound.wav");  //Here
            toastNode.AppendChild(audio);

            ToastNotification toast = new ToastNotification(toastXml);
            ToastNotificationManager.CreateToastNotifier().Show(toast);
}

这篇关于UWP 自定义 Toast 通知声音无法在移动设备上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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