This is my code, I took it from an example from MSDN forum (https://social.msdn.microsoft.com/Forums/ie/en-US/ddb1b7f1-e988-40c7-8e1e-eaf6d8573ec2/uwp-how-to-play-sound-from-wav-fileresource?forum=wpdevelop).
private DispatcherTimer timer;
private TimeSpan myTime = new TimeSpan(0, 0, 60);
public MainPage()
{
this.InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(Object sender, object e)
{
if (myTime.Seconds > 0)
{
myTime -= new TimeSpan(0, 0, 1);
MainTextBlock.Text = myTime.ToString();
}
else
{
timer.Stop();
MainTextBlock.Text = "Finished";
PlaySound_Async();
}
}
private async void PlaySound_Async()
{
MediaElement timesup = new MediaElement();
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("timesup.mp3");
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
timesup.SetSource(stream, file.ContentType);
timesup.Play();
}
And this is the exception I get this exception
I have already added the file in the right folder
Just adding files to the Assets
folder in File Explorer is not a complete step of importing file into the project. You also need to add the file to the Assets
in the Solution Explorer of Visual Studio. You can check the following steps to import the file:
Show All Files
option in the Solution Explorer, and find the timesup.mp3
file in the Assets
folder.timesup.mp3
file and select the Include In Project
option.timesup.mp3
file and ensure the Build Action
is set to Content
in the Properties window.In addition to using the above steps to import a file to Assets
, you can also right-click Assets
and select Add> Existing item…, select the target file and click Add
button to import a file.
Furthermore, if you want to know how to look at the output of the folder variable, please refer to the document.