Repository navigation
PlaySync
James Brundage edited this page Oct 23, 2026
·
1 revision
Wave
On Linux, uses aplay to play.
On MacOS, used 'afplay' to play.
On Windows, uses [Media.SoundPlayer]
View Source
<#
.SYNOPSIS
Plays a Wave Synchronously
.DESCRIPTION
Plays a Wave file and waits for the play to complete.
.NOTES
On Linux, uses `aplay` to play.
On MacOS, used 'afplay' to play.
On Windows, uses `[Media.SoundPlayer]`
.LINK
https://learn.microsoft.com/en-us/dotnet/api/system.media.soundplayer?wt.mc_id=MVP_321542
.LINK
https://linux.die.net/man/1/speaker-test
#>
[OutputType('Wave')]
param()
$currentWave = $this
# If we know what tune to play, but haven't played it yet,
if ($currentWave.Melody -and -not $currentWave.Duration) {
$currentWave = $this.Sound() # render the wave.
}
if ($IsLinux) {
$aplay =
$ExecutionContext.SessionState.InvokeCommand.GetCommand('aplay','Application')
if ($aplay) {
$filePath = $currentWave.Save().FullName
$null = aplay $filePath
} else {
Write-Warning "aplay not found, cannot play sound on Linux"
}
} elseif ($IsMacOS) {
$afplay =
$ExecutionContext.SessionState.InvokeCommand.GetCommand('afplay','Application')
if ($afplay) {
$filePath = $currentWave.Save().FullName
$null = afplay $filePath
} else {
Write-Warning "afplay not found, cannot play sound on MacOS"
}
} else {
if (-not ('Media.SoundPlayer' -as [type])) {
Add-Type -AssemblyName System.Windows.Extensions
}
$null = $currentWave.Seek('0', 'begin')
$soundPlayer = [Media.SoundPlayer]::new($currentWave)
$soundPlayer.PlaySync()
}
return $thisIf you like making waves, please star this repo and consider sponsoring