Play a Sound in Universal Dashboard

Image Description

Daily PowerShell #70

PowerShell Universal Universal Dashboard Daily PowerShell

August 9, 2022

quote Discuss this Article

This blog post was requested by one of our users. Feel free to contact us if you have something you’d like to see.

To play sounds in Universal Dashboard, we’ll first need to host the sound files and then invoke some JavaScript to play the sound.

First, create a published folder that points to your media directory. I have a notification WAV file stored in this folder.

New-PSUPublishedFolder -RequestPath "/media" -Path "C:\ProgramData\UniversalAutomation\Repository\media"

Next, we can create a button on our dashboard to create a new Audio object and play it when the user clicks a button.

New-UDDashboard -Content {
    New-UDButton -Text 'Notification' -OnClick {
        Invoke-UDJavaScript -JavaScript 'var audio = new Audio("/media/notification.wav");
        audio.loop = false;
        audio.play(); '
    }
}

It’s also possible to play the sound after some sort of processing or event takes place. For example, you could use New-UDDynamic to load some data and then play a sound once it completes.

New-UDDynamic -Content {
    Start-Sleep 5
    Show-UDToast "Notification!"
    Invoke-UDJavaScript -JavaScript 'var audio = new Audio("/media/notification.wav");
    audio.loop = false;
    audio.play(); '
}