更改Windows phone 8.1应用开发中toast通知的默认声音
本文关键字:通知 toast 默认 声音 应用开发 Windows phone 更改 | 更新日期: 2023-09-27 18:06:28
我正在使用下面的代码为windows phone 8.1应用程序实现toast通知。我必须将默认声音更改为Assets文件夹中的声音。有人能帮帮我吗?
ToastTemplateType toastType = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastType);
XmlNodeList toastTextElement = toastXml.GetElementsByTagName("text");
toastTextElement[0].AppendChild(toastXml.CreateTextNode("WeCare says: "));
toastTextElement[1].AppendChild(toastXml.CreateTextNode(initialTime + " seconds left"));
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
XmlElement audio = toastXml.CreateElement("audio");
// audio.SetAttribute("src", "/Assets/N.mp3");
((XmlElement)toastNode).SetAttribute("duration", "long");
//((XmlElement)toastNode).SetAttribute("loop", "true");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
确保您在XML中指定的文件存储在适当的位置。Toast with sound仅适用于Windows Phone 8 Update 3及更高版本。还要确保声音文件的长度不超过10秒。
请看这里的例子
请确保使用正确的xml格式的吐司通知
public void ShowToastWithCloudService(bool useCustomSound, bool useWavFormat, bool doSilentToast)
{
StringBuilder toastMessage = new StringBuilder();
toastMessage.Append("<?xml version='"1.0'" encoding='"utf-8'"?><wp:Notification xmlns:wp='"WPNotification'"><wp:Toast>");
toastMessage.Append("<wp:Text1>Toast Title</wp:Text1>");
toastMessage.Append("<wp:Text2>Toast Content</wp:Text2>");
if ((IsTargetedVersion) && (useCustomSound))
{
if (useWavFormat)
{
toastMessage.Append("<wp:Sound>MyToastSound.wav</wp:Sound>");
}
else
{
toastMessage.Append("<wp:Sound>MyToastSound.mp3</wp:Sound>");
}
}
else if ((IsTargetedVersion) && (doSilentToast))
{
toastMessage.Append("<wp:Sound Silent='"true'"/>");
}
toastMessage.Append("</wp:Toast></wp:Notification>");
}