Created
July 17, 2012 00:30
-
-
Save benfulton/3126116 to your computer and use it in GitHub Desktop.
Reading Windows Media Player songs in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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