GITHUB - LINKEDIN - EMAIL
10-09-2017

Music Alarm Script

A friend asked me to create a script that will play music for him in the morning when he wants to wake up. I used powershell and created something that works, for the most part. Testing it overnight had some bugs, looks like it didn’t want to wake up on its own. I need to work out the kinks…

$filepath = (get-location).Path
echo 'What time do you want to wake up? Example "00:00 AM". Enter Below:'
$userTime = Read-Host
clear
echo "Alarm Set for $userTime"
$song = Get-ChildItem -Path "$filepath\musicToPlay" -Include "*mp3" -Recurse
$timeWait = New-Timespan -End $userTime

Start-Sleep -s $timeWait.TotalSeconds

Add-Type -AssemblyName presentationCore
 $song | foreach {
    $wmplayer = New-Object System.Windows.Media.MediaPlayer
    $timeNow = Get-Date -Format g
    echo "The time is now $timeNow"

    echo ("About to play: " + $_.Name)
    $wmplayer.Open("$_")
    Start-Sleep 2 # This allows the $wmplayer time to load the audio file
    $duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds
    $wmplayer.Play()
    echo ("Playing " + $_.Name + "......")
    echo ("Duration == " + $duration)
    Start-Sleep $duration
    $wmplayer.Stop()
    $wmplayer.Close()
 }
©Larry Buffaloboy