Playing Media via Media Player Automation
April 20, 2010 •
On a few occasions I’ve needed to be able to play and control playback of media files – especially music files. There are a number of different ways to do this.
The simplest way to simply play a media file of any kind is by using the ShellExecute API:
gourl("C:\my music\Uploads\Anthrax\Persistence Of Time\08 Got The Time.mp3")
where GoUrl() is my trusted ShellApi wrapper function:
****************************************************
FUNCTION GoUrl
******************
*** Author: Rick Strahl
*** (c) West Wind Technologies, 1996
*** Contact: rstrahl@west-wind.com
*** Modified: 03/14/96
*** Function: Starts associated Web Browser
*** and goes to the specified URL.
*** If Browser is already open it
*** reloads the page.
*** Assume: Works only on Win95 and NT 4.0
*** Pass: tcUrl - The URL of the site or
*** HTML page to bring up
*** in the Browser
*** Return: 2 - Bad Association (invalid URL)
*** 31 - No application association
*** 29 - Failure to load application
*** 30 - Application is busy
***
*** Values over 32 indicate success
*** and return an instance handle for
*** the application started (the browser)
****************************************************
LPARAMETERS tcUrl, tcAction, tcDirectory, tcParms
IF EMPTY(tcUrl)
RETURN -1
ENDIF
IF EMPTY(tcAction)
tcAction = "OPEN"
ENDIF
IF EMPTY(tcDirectory)
tcDirectory = SYS(2023)
ENDIF
DECLARE INTEGER ShellExecute ;
IN SHELL32.dll ;
INTEGER nWinHandle,;
STRING cOperation,;
STRING cFileName,;
STRING cParameters,;
STRING cDirectory,;
INTEGER nShowWindow
IF EMPTY(tcParms)
tcParms = ""
ENDIF
DECLARE INTEGER FindWindow ;
IN WIN32API ;
STRING cNull,STRING cWinName
RETURN ShellExecute(FindWindow(0,_SCREEN.caption),;
tcAction,tcUrl,;
tcParms,tcDirectory,1)
This works nicely in bringing up Media Player (or whatever your default media player is for a given media type) and immediately playing that content. The downside of this very simple approach is that you have no control over the played media – the content starts playing but you have no connection to the player in any way so you can’t start and stop the content under program control. The player is also always visible.
More Control with Media Player Automation
If you want more control over playing media, like the ability to start and stop the content or by creating full custom playlists you can use Media Player automation. This is a little more work but it gives you full control over the process and it’s reasonably lightweight and very responsive even though it’s working through the full Media Player APIs of Windows.
Here’s a simple example that starts and stops two MP3 files:
LOCAL loPlayer as WMPlayer.OCX
loPlayer = CREATEOBJECT("WMPlayer.OCX")
loItems = loPlayer.MediaCollection
loSong= loItems.add("C:\my music\Uploads\Anthrax\Persistence Of Time\08 Got The Time.mp3")
LOCAL loPlayList as WMPlayer.IWMPPlaylist
loPlayList = loPlayer.currentPLayList
loPlayList.AppendItem(loSong)
loPlayer.Controls.PLay()
WAIT WINDOW "Press any key to stop"
loPlayer.Controls.Stop()
*** Clear the playlist for the next song
loPlayList.clear()
*** Add another song
loSong = loItems.add("C:\my music\Uploads\Anthrax\Persistence Of Time\02 Blood.mp3")
loPlayList.AppendItem(loSong)
loPlayer.Controls.Play()
WAIT WINDOW "press any key to stop"
loPlayer.Controls.Stop()
There are many more options on the Media Player control – you can set including controlling the player itself (like volume, display options etc.) as well as full control over the playlist management. It’s quite nice to be able to control all of this with very little code. Unfortunately the documentation for this stuff can’t be found anywhere online – the best option I found was to use Intellisense and pick through the methods and properties that way. The only way to get full documentation is through the SDK docs.
Posting this mainly as a note to self, since I’ve researched this a few times over and over again when I needed as I forgot to write it down. Next time I won’t have to right? <s>
hm@zam.net
July 01, 2010