How do I play mp3 with Powershell?
What I tried:
powershell -c (New-Object Media.SoundPlayer "Track.mp3").Play();
How do I play mp3 with Powershell?
What I tried:
powershell -c (New-Object Media.SoundPlayer "Track.mp3").Play();
You can play an mp3 from powershell, you just need to use
system.windows.media.mediaplayer
instead of
System.Media.SoundPlayer
It works in Windows 10
I followed a snipped from http://eddiejackson.net/wp/?p=9268
# Play a single file
Add-Type -AssemblyName presentationCore
$mediaPlayer = New-Object system.windows.media.mediaplayer
$mediaPlayer.open('C:\sounds\bike.mp3')
$mediaPlayer.Play()
This is an old question but I just went through this so if anyone is interested in learning or knowing how to do this in a more simple manner use the following answer.
As long as you have a media player that has good command line switches such as VLC and other similar types of versatile players (perhaps media player classic) you CAN make a one line command to run a MP3 file. Here is my example, enjoy! :)
& 'C:\Program Files\VideoLAN\VLC\vlc.exe' --qt-start-minimized --play-and-exit --qt-notification=0 "D:\SystemSettings\51Hz.mp3"
This would run 51Hz.mp3 file with neither notifications to take place nor any other user interaction.
This works only for wav files:
$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "path\to\track.wav"
$Song.Play()
From URL
$MediaPlayer = [Windows.Media.Playback.MediaPlayer, Windows.Media, ContentType = WindowsRuntime]::New()
$MediaPlayer.Source = [Windows.Media.Core.MediaSource]::CreateFromUri('https://nyanpass.com/nyanpass.mp3')
$MediaPlayer.Play()
From File
$MediaPlayer = [Windows.Media.Playback.MediaPlayer, Windows.Media, ContentType = WindowsRuntime]::New()
$MediaPlayer.Source = [Windows.Media.Core.MediaSource]::CreateFromUri('C:\Users\Admin\Downloads\nyanpass.mp3')
$MediaPlayer.Play()
Simplest way is to just use the Invoke-Item cmdlet, it will open whatever Windows has set as the default handler for the file type. When I used it on an mp3 file it opened Groove Music. I'm sure if I installed winamp or mpc or whatever and set the default to that it would open in there instead.
SoundPlayer can only play *.wav files. There is no easy 1 line way of playing mp3 files. However this can be done by writing a script using the MediaPlayer class.
This worked for me
$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "path\to\track.mp3"
$Song.Play()
I guess you cannot pass the file location through new-object
I can confirm this works:
$player = New-Object System.Media.SoundPlayer "$env:windir\Media\notify.wav"
$player.Play()
Source: http://community.idera.com/powershell/powertips/b/tips/posts/playing-wav-files