0
votes

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 Assets folder

Exception

2

2 Answers

1
votes

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:

  1. Click Show All Files option in the Solution Explorer, and find the timesup.mp3 file in the Assets folder.

Solution Explorer

  1. Right click the timesup.mp3 file and select the Include In Project option.

Context Menu

  1. Click the timesup.mp3 file and ensure the Build Action is set to Content in the Properties window.

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.

0
votes

If you set a breakpoint on GetFolderAsync, you should be able to look at the folder variable and see where it thinks the Path is... is it the same absolute path that you expect it to be (C:\Users....repos\Multi-timer)