简体   繁体   English

在wp8.1 rt中播放来自应用的通知音

[英]Play notification tone from app in wp8.1 rt

We have a requirement where we need to play notification tone inside app when message is received. 我们有一个要求,当收到消息时,我们需要在应用程序内播放通知音。

For this if a song is getting played in background and notification comes then behaviour should be: 为此,如果一首歌曲正在后台播放并且发出通知,则行为应为:

  1. Background song/audio should get paused. 背景歌曲/音频应暂停。
  2. Notification tone should get played. 通知音应播放。
  3. Background song should get resumed from where it was paused. 应该从暂停的位置恢复背景歌曲。

But I went through following links to play audio file through app: 但是我通过以下链接通过应用程序播放音频文件:

1) http://www.jayway.com/2014/04/24/windows-phone-8-1-for-developers-the-background-media-player/ This stops music in background. 1) http://www.jayway.com/2014/04/24/windows-phone-8-1-for-developers-the-background-media-player/这会在后台停止音乐。

2) http://blogs.msdn.com/b/johnkenn/archive/2013/12/31/supporting-background-audio-in-your-windows-8-1-app.aspx 2) http://blogs.msdn.com/b/johnkenn/archive/2013/12/31/supporting-background-audio-in-your-windows-8-1-app.aspx

3) https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.media.systemmediatransportcontrols.aspx 3) https://msdn.microsoft.com/zh-CN/library/windows/apps/xaml/windows.media.systemmediatransportcontrols.aspx

But none of these served my purpose. 但是这些都不符合我的目的。

In wp8, we could do it in this way . 在wp8中,我们可以用这种方式做到这一点。

Any direction to achieve desired functionality? 任何实现所需功能的方向?

If you are sending toast notifications to your users when they receive a message, include a custom sound in your notification payload by including the audio tag: 如果您是在用户收到消息时向他们发送烤面包通知,请通过添加audio标签在通知有效负载中包含自定义声音:

    <toast launch=\"$param\">
       <audio src=\"ms-appx:///Assets/Sounds/$sound.wav\"/>
       <visual>
           <binding template=\"ToastText02\">
               <text id=\"1\">$title</text>
               <text id=\"2\">$msg</text>
           </binding>
       </visual>
    </toast>

For the custom sound to work, you need to include the sound.wav in your project and you can specify the path. 为了使自定义声音起作用,您需要在项目中包含sound.wav并可以指定路径。 I am putting it in my Assets/Sounds/ folder, you can specify something else. 我将其放在我的Assets/Sounds/文件夹中,您可以指定其他内容。

You can also try to send a toast with sound, from within you app when you receive a message: 当您收到消息时,也可以尝试在应用内发送带有声音的祝酒词:

var notificationContent = "...xml...";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(notificationContent);
var toastNotification = new ToastNotification(xmlDocument);
toastNotification.SuppressPopup = true;
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);

where notificationContent is of the same format as the above XML, just escaped. 其中notificationContent与上述XML的格式相同,只是转义了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM