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

David has posted 23 posts at DZone. View Full User Profile

Automating Windows Media Player With Ruby

  • submit to reddit
        From the <a href="">Ruby on Windows</a> blog.

require 'win32ole'
player ='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)

#   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

#   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)

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

#   Remove a song from a playlist

#   Play a playlist
playlist = playlists.getByName('Frank & Dino').Item(0)

Further details can be found <a href="">here</a>.