4
votes

I am stuck with a toast notification issue in Windows phone 8.1. (Exact version: 8.10.14157.200). I have an application that sends toast notification to the phone. I want to play a custom sound with this toast notification. However windows phone just plays its default notification sound and not my own that I am specifying in the toast XML.

<?xml version="1.0" encoding="UTF-8"?>
<toast duration="long">
   <visual>
      <binding template="ToastText02">
         <text id="1">Kainat</text>
         <text id="2">Hi</text>
      </binding>
   </visual>
 <audio src="/Assets/hodor.wav" />
</toast>

I have tried the following audio elements with no success.

<audio src="hodor.wav" />
<audio src="/hodor.wav" />
<audio src="/Assets/hodor.wav" />
<audio src="ms-appx:///Assets/hodor.wav" />
<audio src="ms-appx:///hodor.wav" />

I have also tried muting (just to verify that the audio element in the XML works)

<audio silent="true"/>

This worked and sound was suppressed.

I have added hodor.wav in my project as well as the Assets folder in my visual studio project. Also have set the "Copy to output directory" option of this wav resource to "Copy always".

Is there anything I am missing?

3
did you ever get this working? If so, how?DaveDev
I'm facing the same issue. Any help here guys?AbsoluteSith
Here is my answer to this issue: stackoverflow.com/questions/33365202/… if someone else is facing this same problemludesign

3 Answers

3
votes

@OliverUlm: MSDN says WP8.1 can use custom sound.

According to one of MSDN page in here. It says:

"On Windows Phone 8.1, this attribute can also contain the path to a local audio file, with one of the following prefixes:

ms-appx:///
ms-appdata:///

But, I still haven't figure out how to play the sound.

UPDATE:

<?xml version="1.0" encoding="UTF-8"?>
<toast>
  <visual>
    <binding template="ToastText02">
      <text id="1">Title</text>
      <text id="2">Message</text>
    </binding>
  </visual>
  <audio src="ms-appx:///Audio/Female/0530.mp3" />
</toast>

It works for me.

0
votes

According to the documentation on MSDN the audio-tag in WNS does not support fully custom sounds in the audio element (contrary to WPNS on WP8.0 GDR3). You can only use the following strings to influence what kind of notification is played (and which are built into the OS).

  • ms-winsoundevent:Notification.Default
  • ms-winsoundevent:Notification.IM
  • ms-winsoundevent:Notification.Mail
  • ms-winsoundevent:Notification.Reminder
  • ms-winsoundevent:Notification.SMS
  • ms-winsoundevent:Notification.Looping.Alarm
  • ms-winsoundevent:Notification.Looping.Alarm2
  • ms-winsoundevent:Notification.Looping.Alarm3
  • ms-winsoundevent:Notification.Looping.Alarm4
  • ms-winsoundevent:Notification.Looping.Alarm5
  • ms-winsoundevent:Notification.Looping.Alarm6
  • ms-winsoundevent:Notification.Looping.Alarm7
  • ms-winsoundevent:Notification.Looping.Alarm8
  • ms-winsoundevent:Notification.Looping.Alarm9
  • ms-winsoundevent:Notification.Looping.Alarm10
  • ms-winsoundevent:Notification.Looping.Call
  • ms-winsoundevent:Notification.Looping.Call2
  • ms-winsoundevent:Notification.Looping.Call3
  • ms-winsoundevent:Notification.Looping.Call4
  • ms-winsoundevent:Notification.Looping.Call5
  • ms-winsoundevent:Notification.Looping.Call6
  • ms-winsoundevent:Notification.Looping.Call7
  • ms-winsoundevent:Notification.Looping.Call8
  • ms-winsoundevent:Notification.Looping.Call9
  • ms-winsoundevent:Notification.Looping.Call10
0
votes

Try this example:

in code:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
        toastTextElements[0].AppendChild(toastXml.CreateTextNode("Test1"));
        toastTextElements[1].AppendChild(toastXml.CreateTextNode("Test2"));
        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        XmlElement audio = toastXml.CreateElement("audio");
        string sound = "test1.mp3";
        if (sound != null)
            audio.SetAttribute("src", "ms-appdata:///local/" + sound);
        else
            audio.SetAttribute("src", "ms-appx:///sounds/Bell.mp3");
        toastNode.AppendChild(audio);
        ToastNotifier toastNotifier =
         ToastNotificationManager.CreateToastNotifier();
        ToastNotification toastNotification = new ToastNotification(toastXml);
        toastNotifier.Show(toastNotification);
    }

with xaml: In the XMLFile1.xml:

     <?xml version="1.0" encoding="UTF-8"?>
     <toast>
      <visual>
       <binding template="ToastText02">
        <text id="1">Test1</text>
        <text id="2">Test2</text>
      </binding>
     </visual>
   <audio src="ms-appdata:///local/test1.mp3"/>
 </toast>

In the MainPage.xaml.cs:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(XDocument.Load("XMLFile1.xml").ToString());
        ToastNotifier toastNotifier =
        ToastNotificationManager.CreateToastNotifier();
        ToastNotification toastNotification = new ToastNotification(xmldoc);
        toastNotifier.Show(toastNotification);
    }