Skip to content

Commit 5704eae

Browse files
committed
Support ms-appx audio
1 parent 80f7871 commit 5704eae

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

Microsoft.Toolkit.Uwp.Notifications/Toasts/Builder/ToastContentBuilder.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,25 +355,20 @@ public ToastContentBuilder AddAudio(
355355
bool? silent = default)
356356
#endif
357357
{
358-
if (!src.IsFile && src.Scheme != "ms-winsoundevent")
359-
{
360-
throw new ArgumentException(nameof(src), "Audio Source has to be a file.");
361-
}
362-
363-
Content.Audio = new ToastAudio();
364-
Content.Audio.Src = src;
358+
var audio = new ToastAudio();
359+
audio.Src = src;
365360

366361
if (loop != default)
367362
{
368-
Content.Audio.Loop = loop.Value;
363+
audio.Loop = loop.Value;
369364
}
370365

371366
if (silent != default)
372367
{
373-
Content.Audio.Silent = silent.Value;
368+
audio.Silent = silent.Value;
374369
}
375370

376-
return this;
371+
return AddAudio(audio);
377372
}
378373

379374
/// <summary>
@@ -383,6 +378,11 @@ public ToastContentBuilder AddAudio(
383378
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
384379
public ToastContentBuilder AddAudio(ToastAudio audio)
385380
{
381+
if (audio.Src != null && !audio.Src.IsFile && audio.Src.Scheme != "ms-appx" && audio.Src.Scheme != "ms-winsoundevent")
382+
{
383+
throw new InvalidOperationException("Audio Source must either be a ms-appx file, absolute file, or ms-winsoundevent.");
384+
}
385+
386386
Content.Audio = audio;
387387
return this;
388388
}

UnitTests/UnitTests.Notifications.Shared/TestToastContentBuilder.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,22 @@ public void AddAudioTest_WithMsWinSoundEvent_ReturnSelfWithCustomAudioAdded()
564564
}
565565

566566
[TestMethod]
567-
[ExpectedException(typeof(ArgumentException))]
567+
public void AddAudioTest_WithMsAppx_ReturnSelfWithCustomAudioAdded()
568+
{
569+
// Arrange
570+
Uri testAudioUriSrc = new Uri("ms-appx:///Assets/Audio.mp3");
571+
572+
// Act
573+
ToastContentBuilder builder = new ToastContentBuilder();
574+
ToastContentBuilder anotherReference = builder.AddAudio(testAudioUriSrc);
575+
576+
// Assert
577+
Assert.AreSame(builder, anotherReference);
578+
Assert.AreEqual(testAudioUriSrc.OriginalString, builder.Content.Audio.Src.OriginalString);
579+
}
580+
581+
[TestMethod]
582+
[ExpectedException(typeof(InvalidOperationException))]
568583
public void AddAudioTest_WithInvalidMsUri_ThrowException()
569584
{
570585
// Arrange
@@ -576,7 +591,19 @@ public void AddAudioTest_WithInvalidMsUri_ThrowException()
576591
}
577592

578593
[TestMethod]
579-
[ExpectedException(typeof(ArgumentException))]
594+
[ExpectedException(typeof(InvalidOperationException))]
595+
public void AddAudioTest_WithInvalidAppDataUri_ThrowException()
596+
{
597+
// Arrange (ms-appdata isn't currently supported)
598+
Uri testAudioUriSrc = new Uri("ms-appdata:///local/Sound.mp3");
599+
600+
// Act
601+
ToastContentBuilder builder = new ToastContentBuilder();
602+
builder.AddAudio(testAudioUriSrc);
603+
}
604+
605+
[TestMethod]
606+
[ExpectedException(typeof(InvalidOperationException))]
580607
public void AddAudioTest_WithInvalidHttpUri_ThrowException()
581608
{
582609
// Arrange

0 commit comments

Comments
 (0)