Search code examples
c#raspberry-pi2windows-10-iot-corewindowsiot

play wav file in Raspberry Pi with Windows 10 IOT Core


Could someone please give me some starting kick on how to play a simple wav file on Raspberry Pi under Windows 10? What I want is load a few files into memory and on specific events, start playing them, if possible, play the sounds at the same time as well. I tried this, but it failed with the DLLImport row (CoreDll.dll not found). So, any clever link or sample is very welcome. Thank you, vm

I created a simple test project, it runs, exits without error, but I only hear silence. Am I doing something wrong? I expected that the code below plays a short drum sample 50 times.

namespace BackTC
{
    public sealed class StartupTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            btnPlayWavSound_Tapped();
        }


        private async void btnPlayWavSound_Tapped()
        {

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/CLudwigKick-Dyn01.WAV"));
            MediaPlayer player = BackgroundMediaPlayer.Current;
            player.SetFileSource(file);


            for (int i = 1; i <= 50; i++)
            {
                player.Volume = 100;
                player.Play();
            }

        }

    }
}

Solution

  • I've got the following code that will work in a Background Application project and it reads the WAV file from the Assets folder. Don't forget to set the WAV file to copy to output directory.

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/filename.wav"));
    MediaPlayer player = BackgroundMediaPlayer.Current;
    player.AutoPlay = false;
    player.SetFileSource(file);
    player.Play();