7

How do I play mp3 with Powershell?

What I tried:

powershell -c (New-Object Media.SoundPlayer "Track.mp3").Play();
Mofi
  • 46,139
  • 17
  • 80
  • 143
Heaven
  • 115
  • 2
  • 2
  • 5
  • 1
    Take a look on [powershell 'system.windows.media.mediaplayer' Register-ObjectEvent](http://stackoverflow.com/questions/17924310) which explains how to play an entire MP3 list. – Mofi Sep 17 '14 at 17:37

9 Answers9

9

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()
Max Carroll
  • 4,441
  • 2
  • 31
  • 31
  • 1
    Recommend accepting this answer. It's the first answer that uses MP3 rather than WAV. – Brad May 29 '19 at 21:20
1

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.

nndhawan
  • 597
  • 6
  • 24
1

This works only for wav files:

$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "path\to\track.wav"
$Song.Play()
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
1

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()
井上智文
  • 1,905
  • 17
  • 14
0

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.

-1

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.

Arun
  • 941
  • 6
  • 13
-1

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

Musa
  • 19
  • 1
  • 1
    Did you even try this, Musa? If so, you would get the following error: Exception calling "Play" with "0" argument(s): "The file located at c:\temp\temp.mp3 is not a valid wave file." At line:1 char:1 + $Song.Play() + ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException – kotpal Nov 07 '20 at 03:37
-1

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

hagan10
  • 153
  • 2
  • 12
-1

I recommend to just write, Start-Process -FilePath "The/MP3/File/Path".

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
DevR3
  • 1
  • 1