Over a million developers have joined DZone.

Automating Windows Media Player With Ruby

DZone's Guide to

Automating Windows Media Player With Ruby

Free Resource
From the Ruby on Windows 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)

#   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 here.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}