Skip to content

Instantly share code, notes, and snippets.

@benfulton
Created July 17, 2012 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfulton/3126116 to your computer and use it in GitHub Desktop.
Save benfulton/3126116 to your computer and use it in GitHub Desktop.
Reading Windows Media Player songs in Ruby
require 'win32ole'
class MediaPlayer
def initialize
@player = WIN32OLE.new('WMPlayer.OCX')
end
def songs
s = @player.mediacollection.getByAttribute("MediaType","Audio")
if block_given?
(0..s.Count - 1).each do |i|
yield Song.new(s.Item(i))
end
else
return (0..s.Count - 1).map { |i| Song.new(s.Item(i))}
end
end
def all
return @player.mediacollection.getAll
end
def byAuthor(author)
return @player.mediacollection.getByAuthor(author)
end
def byAlbum(author)
return @player.mediacollection.getByAlbum(author)
end
end
class Song
def initialize(wmpsong)
@wmpsong = wmpsong
end
def Name
@wmpsong.Name
end
def Author
return @wmpsong.getItemInfo("Author");
end
def Attributes
return (0..@wmpsong.attributeCount - 1).map { |i| @wmpsong.getAttributeName(i)}
end
end
player = MediaPlayer.new()
player.songs.each{ |s| puts s.Name + " - -" + s.Author }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment