Rick Strahl's Weblog
Rick Strahl's FoxPro and Web Connection Weblog
White Papers | Products | Message Board | News |

Playing Media via Media Player Automation


4 comments
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>

Posted in:

Feedback for this Weblog Entry


re: Playing Media via Media Player Automation



hm@zam.net
July 01, 2010

Did you discover anyway to also record in MP3? If I have the LAME driver on the system, I would think this might be possible but not sure if media player can also record.

--jim

re: Playing Media via Media Player Automation



Rick Strahl
July 01, 2010

I think you can make Media Player Rip a CD from songs, but not record music.

The Lame driver is a converter and it's a DLL you can access fairly easily with WinAPI code in Fox. However, that doesn't help with recording first. Audio recorder might be automatable but there are probably better solutions out there in the form of ActiveX controls. Jim Murez (Harvey Mushman) had been playing around with some of this some time ago and I remember the controls he used weren't very pricey.

re: Playing Media via Media Player Automation



jlwAnsori
April 27, 2012

Thanks for the info knowledge about MP 3 play music through visual foxpro Excuse me put on my web

re: Playing Media via Media Player Automation



Bill
February 12, 2013

Rick, Thanks for the code - it works well. I'm copying an MP3 to a Memo then to a temp file, playing the temp file but can't delete the temp file. I understand that .stop() doesn't close wmplayer, just the current MP3. Is there a "close()" for wmplayer or some other solution for this.

I've searched around and I found the following but it doesn't work (hwindow=0)

#DEFINE WM_SYSCOMMAND 0x112 #DEFINE SC_CLOSE 0xF060

DECLARE INTEGER FindWindow IN user32; INTEGER lpClassName, STRING lpWindowName

DECLARE SHORT PostMessage IN user32; INTEGER hWnd, INTEGER Msg, INTEGER wParam,; INTEGER lParam

LOCAL hWindow

IF hWindow <> 0 = PostMessage(hWindow, WM_SYSCOMMAND, SC_CLOSE, 0) ENDIF

I also found this but can't put it into Foxspeak var prc = Process.GetProcessesByName("wmplayer"); if (prc.Length > 0) prc[prc.Length - 1].Kill();

 
© Rick Strahl, West Wind Technologies, 2003 - 2024