How To: Control Windows Media Player
The question has come up a couple of time in various forum threads about how to control the Media Player. We have learned many things about how to control the Media Player through these threads, so it's time we make a tutorial.
One caveat is that when we talk about the Media Player, we are referring to the Desktop Media Player version, not the "funky" Windows 8 app Media Player.
*With all code examples, change the path and file name of directories as needed!
First, Windows Media Player by design is limited in the commands it can accept via a command line option, and this is really the fundamental way to control an application like the Media Player in Windows. Microsoft explains what is available: https://support.microsoft.com/en-us/kb/241422
Second, we learned that you can't directly launch Media Player or a Play List from a Script in ARC without using a batch file. (The Batch file would include the instructions to open Media Player or a Play List.)
Third, we learned you can call music file to play directly in ARC without placing the sound file in a SoundBoard:
PlayAudio(d:\Music\ACDCBallBreaker\BurninAlive.mp3")
Forth, we learned you can call a Video File and it will automatically open in Media Player if that is set as your default player:
Exec("D:\Movies\StarWars.wmv")
Fifth, we learned there is a method for granular control of the alternative media player called "WinAmp" using another 3rd party application control "CLEver". This thread discusses that method.
Sixth, we learned how we can control Windows Media Player to play a play list, stop, play, pause, next track, previous track, mute, unmute, volume up, volume down and close by using a combination of built in Script functions, Batch (.bat) files and PowerShell (.ps1) script files.
Batch files are a long time standard of Windows and dates back to DOS as a method to automate and script basic functions in the OS.
PowerShell is scripting language from Microsoft which is free and comes packaged with Windows7 and 8.x. It utilizes .Net framework and is used in the IT profession to help manage windows systems. It can be a powerfull and easy way to add external controls to ARC that ARC does not currently include.
The sample files you'll need including the batch files and power shell files can be found here under Software: http://www.j2rscientific.com
To open a Play List with Media Player, you need to create a play list under Windows Media Player, then create a short cut file (.lnk) of the play list. ARC can use this to launch the play list. Use this ARC Script:
Exec("C:\Users\weyou_000\Documents\EZ-Builder\My Projects\ShortCuts\All 80's.lnk")
To close Media Player, use this ARC Script:
$attributes = "/F /IM wmplayer.exe"
exec("taskkill",$attributes)
Example of Batch file (.bat) code to call a PowerShell script (because ARC can't do it directly):
powershell -ExecutionPolicy RemoteSigned -File "C:\Users\weyou_000\Documents\EZ-Builder\My Projects\PowerShell\Mute_Unmute.ps1"
Example of PowerShell script to control mute and unmute:
$wshShell = new-object -com wscript.shell
$wshShell.SendKeys([char]173)
The PowerShell script is sending a keyboard character 173, which is the code to mute/unmute. The follow are the key codes used in the various PowerShell Scripts:
Mute/Unmute: 173 Volume Down: 174 Volume Up: 175 Next Track: 176 Previous Track: 177 Stop Media: 178 Play/Pause: 179 These key commands were found here.
Example of ARC script to call the Batch file to control mute and unmute:
Exec("C:\Users\weyou_000\Documents\EZ-Builder\My Projects\BatchFiles\Mute_Unmute.bat")
Please watch the YouTube video tutorial:
Great tutorial Justin, and the upcoming video will be interesting to watch and will be very useful. If I get time over the weekend I'll give this a try on my Acer W3.
Thanks for taking the time to post the tutorial. Its well written, and I'm sure others will find it very useful (including myself). Good job.
Steve.
Thank you Steve. Video is uploaded. It might dive in deeper than some need and perhaps less then others need. Post any questions you have.
Uau! Justin, you're the man! Thank you so much!
man I'm getting a hard time here... the commands that use PowerShell does not works.. I made all the path modifications.. but nothing! any clue ?!
Yes you need to modify all paths in the .bat batch files. Trying opening the Powershell ise, like I show in the video and try running the mute unmute script directly in Powershell. If you get an error message please screen shot and share it.
Thanks.
oks... here is what I get.. this is the Next track command. I tried with the mute/unmute and the results are the same..
It is a security setting on your computer.
You can bypass this policy by adding -ExecutionPolicy ByPass when running PowerShell
powershell -ExecutionPolicy ByPass -File script.ps1
There are other options also.
About execution policies
Ok, that is for next track, that is cool. This is something others might see as well if you try to run the script directly from PowerShell as the execution policy is often set to a restrictive setting by default. If you click on the "Untitled1.ps1" tab or click File/New for a new tab, enter in the follow and click Run in PowerShell:
This will probably return "Restricted". Clear that code out or open a new file/tab and enter and run the following code in PowerShell:
This sets the Execution Policy to allow you to run PowerShell scripts in PowerShell. This should work as long as your user account is set to have administrator rights. After you run that, clear it out and enter and run the following:
This should return "RemoteSigned"
This process replicates what is in the Batch file (.bat) to run the PowerShell script. If we look at the Batch file named: NextTrackMediaPlayer.bat we see this code:
The first part of the code tells the batch file to use PowerShell with the Execution Policy of RemoteSigned, which if you ran the code above you now have RemoteSigned policy setting in PowerShell directly.
The second part of the batch file is what you need to change to reflect your path for the PowerShell script and from your screen shot it looks like your total script in the Batch file should be this:
Is that what you have in your Batch file named "NextTrackMediaPlayer.bat"?
yes! that's exactly.. for some reason, the ExecutionPolicy is not working... or.. the Key code ([char]176) is doing nothing at all..
When you run this in PowerShell, what is returned?
"RemoteSigned"
Go, that's good. Now with MediaPlayer open, playing a music track and with more than one track in the play list, what happens when you run the PowerShell script for "Next_Track.ps1"?
nothing... it does not jump the track...
I tryed to change my keyboard settings.. to English. (mabe the codes are different for Brazilian ABNT keyboard). but it did not worked too..
Do any of the PowerShell scripts work for you like mute, previous, play/pause?
Does the script in ARC work to open the Windows Media Player?
Do you have next/previous play/pause keys on your keyboard? If so, can you manually press those keys to control the Windows Media Player?
Ok... I just tested with an Windows 8 PC (virtual MAchine) and it workrd Great! so.. mabe the problem is with windows 7 codes... Mabe ?..
Can you show a screen shot of your Media player under Win7?
I ran the scipts under my Win7 virtual machine and it ran fine once I set the ExecutionPolicy to RemoteSigned. The codes are the same across Windows versions.
Under your Windows 7 machine do you have next/previous play/pause keys on your keyboard? If so, can you manually press those keys to control the Windows Media Player?
ok.. The mute/unmute is working on windows 7. The "previuou Track", also working..
But the "Next Track", The "play_PAuse" and the others are not!... Since its a virtual Machine, the Buttons on the keyboard does not control the Windows Media Player.. So It migth be some difference on code numbers.. don't you think ?
The keyboard codes remain the same. In my virtual machines they accept my keyboard controls for the Media Player. I use VMware Workstation.
It's the same that I use! What's going wrong here... I don't understand. stress stress stress I'll try to uninstall the media player and instal it again.. It does not make sense.. But, it's windows after all.. Any other suggestion ?
It does not make sense that some of the scripts would work and some would not.
You might want try the alternative method Steve G. shared using WinAmp and CLEver.exe.
FOUND THE SOLUTION!
Its so simple... since I had My Main Language set to Portuguese, the Key Commands where different.. So I just set my main Language and keyboard to English, and... That's IT! I still got my Portuguese and language keyboard installed, and working...
And now my Robot controls Media Player with your Scripts! Thanks a lot!
That is great to know tevans, I'm so glad you found the solution!
Multilanguage support is not my strong suite unfortunately.