DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Automating Windows Media Player With Ruby
From the <a href="http://rubyonwindows.blogspot.com">Ruby on Windows</a> blog.
require 'win32ole'
player = WIN32OLE.new('WMPlayer.OCX')
media_collection = player.mediaCollection
playlists = player.PlaylistCollection
# Play a song
player.OpenPlayer('c:\music\van halen\right now.wma')
# Select songs from the Media Collection
all_media = media_collection.getAll()
audio_media = media_collection.getByAttribute("MediaType", "Audio")
sinatra_songs = media_collection.getByAuthor("Frank Sinatra")
album = media_collection.getByAlbum('Come Fly with Me')
jazz_tunes = media_collection.getByGenre('Jazz')
songs = media_collection.getByName('Fly Me to the Moon')
# Play the first song
first_song = songs.Item(0)
player.OpenPlayer(first_song.sourceURL)
# Add a song to the Media Collection
song = media_collection.Add('C:\music\Just in Time.wma')
# Remove a song from the Media Collection
songs = media_collection.getByName('Fly Me to the Moon')
media_collection.Remove(songs.Item(0), true)
# Select a playlist
all_playlists = playlists.getAll()
split_enz_playlist = playlists.getByName('Split Enz')
# Iterate over songs in a playlist
(0..my_playlist.Count - 1).each do |i|
song = my_playlist.Item(i)
puts song.Name
end
# Create a new playlist
playlists = player.PlaylistCollection
playlists.newPlaylist('New Playlist')
media_collection.Add('D:\Music\My Playlists\New Playlist.wpl')
# Remove a playlist
playlists = player.PlaylistCollection
split_enz_playlist = playlists.getByName('Split Enz').Item(0)
playlists.Remove(split_enz_playlist)
# Add a song to a playlist
song = media_collection.getByName('Fly Me to the Moon').Item(0)
playlist = playlists.getByName('Frank & Dino').Item(0)
playlist.appendItem(song)
# Remove a song from a playlist
playlist.removeItem(song)
# Play a playlist
playlist = playlists.getByName('Frank & Dino').Item(0)
player.OpenPlayer(playlist.sourceURL)
Further details can be found <a href="http://rubyonwindows.blogspot.com/2007/07/automating-windows-media-player-with.html">here</a>.




