Sunday, July 29, 2007

Automating Windows Media Player with Ruby

My recent article about automating iTunes has resulted in several requests for a similar article about Windows Media Player (WMP). And so, without further ado...

We start by using the win32ole library to connect to the WMPlayer object:


require 'win32ole'
player = WIN32OLE.new('WMPlayer.OCX')

Note that this will not display the WMP user interface. That happens next.

To play a song, call the player object's OpenPlayer method, passing it the path to the song file:

player.OpenPlayer('c:\music\van halen\right now.wma')

The Media Collection

Next, we'll grab the MediaCollection object:

media_collection = player.mediaCollection

To quote Microsoft, "The MediaCollection object represents all the items stored in the Windows Media Player media collection. You will typically query the MediaCollection object to return collections of media items."

The MediaCollection object's getAll method returns a collection of all items in the library:

all_media = media_collection.getAll()

The getByAttribute method can be used to get a collection of all audio files:

audio_media = media_collection.getByAttribute("MediaType", "Audio")

To get a collection of songs by a specific artist, use the getByAuthor method:

sinatra_songs = media_collection.getByAuthor("Frank Sinatra")

You can also get items by Album:

album = media_collection.getByAlbum('Come Fly with Me')

Or by Genre:

jazz_tunes = media_collection.getByGenre('Jazz')

And, of course, you can get a collection of items by name:

songs = media_collection.getByName('Fly Me to the Moon')

Note that this will return a collection of items, even if it contains only one item. You reference a specific item by calling the collection's Item method, passing it a (zero-based) index value. So to get the first item:

first_song = songs.Item(0)

As mentioned earlier, to play a song, call the player object's OpenPlayer method, passing it the song object's sourceURL property. sourceURL is simply the path to the song file:

player.OpenPlayer(first_song.sourceURL)

To add a song to your Media Collection, call the MediaCollection object's Add method, passing it the path and filename:

song = media_collection.Add('C:\music\Just in Time.wma')

To delete a song, from the Media Collection, call the MediaCollection object's Remove method, passing it the song object and the boolean value 'true':

songs = media_collection.getByName('Fly Me to the Moon')
media_collection.Remove(songs.Item(0), true)

You may need to restart the Media Player user interface to see additions and removals.

The PlayLists Collection

PlayLists are managed via the player's PlaylistCollection object:

playlists = player.PlaylistCollection

The PlaylistCollection object has methods similar to the MediaCollection object. Its getAll method returns a collection of all PlayList objects:

all_playlists = playlists.getAll()

Its getByName method returns a collection of PlayList objects by name:

split_enz_playlist = playlists.getByName('Split Enz')

Remember, this returns a collection, even if that collection contains a single item. So to get the first (and possibly only) item, we call the Item method on the returned collection:

split_enz_playlist.Item(0)

A PlayList is a collection of Song items, just like those returned by the MediaCollection object's methods. You can call each item in the collection by index. So to print the name of each song in our playlist:

(0..my_playlist.Count - 1).each do |i|
song = my_playlist.Item(i)
puts song.Name
end

To create a new playlist, call the PlaylistCollection object's newPlaylist method, passing it the name of the new playlist:

playlists = player.PlaylistCollection
playlists.newPlaylist('New Playlist')

This creates a .wpl playlist file. You then have to add the new playlist file to the MediaCollection object, by calling the Add method and passing it the path and name of the newly created PlayList file:

media_collection.Add('D:\Music\My Playlists\New Playlist.wpl')

The path to the playlist file will be defined in the 'Rip music to this location' setting in Media Player's options.

To remove a playlist, call the PlaylistCollection object's Remove method, passing it the name of the PlayList object:

playlists = player.PlaylistCollection
split_enz_playlist = playlists.getByName('Split Enz').Item(0)
playlists.Remove(split_enz_playlist)

To add a song to a playlist, call the PlayList object's appendItem method, passing it the song object:

song = media_collection.getByName('Fly Me to the Moon').Item(0)
playlist = playlists.getByName('Frank & Dino').Item(0)
playlist.appendItem(song)

To remove a song from a playlist, call the PlayList object's removeItem method, passing it the song object:

playlist.removeItem(song)

Playing a playlist is much like playing an individual song. First get the individual PlayList object, then pass its sourceURL value to the player object's OpenPlayer method:

playlist = playlists.getByName('Frank & Dino').Item(0)
player.OpenPlayer(playlist.sourceURL)

And there you have it.

Further details can be found in this Microsoft TechNet article.

As always, feel free to post a comment or send email with questions, comments, or suggestions.

Thanks for stopping by!

Digg my article

4 comments:

Charles Roper said...

Awesome, thanks! This gives me enough info to try something I've wished MS would include in WMP for ages: the ability to scan through the library removing items that have been physically removed from the disk.

Anonymous said...

If I want to close the WMP, how should I do?

decaffeine said...

It's great. Can somebody tell me how to get controls working. I've tried everything but couldn't make play() method to work.

Unknown said...

This one will help many users like us. Thanks for sharing. Was looking for it since a long time.

mp4 players