Hi,
I am trying to build a simple application that needs to play mp3 files.
Has anyone ever done this with Ruby ? I guess I would need to do some
Windows Media player ‘remote control’, but, assuming this is an option,
I do not where to start. Any guidance, idea, link, whatsoever would be
extremely helpful.
Tia,
Jb
On Thu, 12 Jan 2006, Jb Piacentino wrote:
Hi,
I am trying to build a simple application that needs to play mp3 files.
Has anyone ever done this with Ruby ? I guess I would need to do some
Windows Media player ‘remote control’, but, assuming this is an option,
I do not where to start. Any guidance, idea, link, whatsoever would be
extremely helpful.
you may be interested in
http://localhost:8808/doc_root/ruby-mp3info-0.4/rdoc/index.html
Wybo D. wrote:
you may be interested in
http://localhost:8808/doc_root/ruby-mp3info-0.4/rdoc/index.html
Yes I got this one, but this only deals with getting infos on the file -
does not help me with playing it !
This sounded like an interesting challenge - here’s a simple interface
to the Windows Media Player:
SimpleWinMediaPlayer.rb
- Adam S., January 2006
require “win32ole”
class SimpleWinMediaPlayer
def initialize
@mp = WIN32OLE.new(“WMPlayer.ocx”)
p @mp
@control = @mp.controls
end
def file= str
@mp.URL= str
@media = @mp.currentMedia
@control.currentItem = @media
end
def file
@mp.url
end
def play
if @control.isAvailable “play”
@control.play
else
@control.playItem @control.currentItem
end
end
def pause
@control.pause
end
def stop
@control.stop
end
def rewind
if @control.isAvailable “fastReverse”
@control.fastReverse
else
seek_to 0
end
end
def fforward
@control.fastForward
end
def current_pos
@control.currentPositionString
end
def seek_to pos
takes argument as S[.ms] or “S[.ms]” or “[MM]:SS[:ms]”
if pos =~ /(\d*):(\d+)(:(\d*))?/
pos = $1.to_f*60+$2.to_f+$4.to_f/1000.0
end
@control.currentPosition = pos.to_f
end
def duration
@media.duration
end
end
basic usage:
player = SimpleWinMediaPlayer.new
player.file = “c:/whatever.mp3”
player.play
Hope this helps
-Adam
I did almost all my testing in irb - where that set of lines worked
perfectly, but when I tried to write a simple jukebox I ran into the
same
problem you did.
There is some timing issue - the player is running in a separate thread
or
process, and it doesn’t respond instantly to the ole calls. The quick
solution I came up with was to add some sleeps between the calls.
There’s
probably a better solution involving checking statuses.
w = SimpleWinMediaPlayer.new
w.volume = volume
songlist.each {|song|
puts (fullname = ‘file://’+dirname+’/’+song)
w.file= fullname
sleep 0.1
w.play
sleep 0.1
puts w.duration
sleep (w.duration)
while w.current_pos != “”
sleep (0.5)
end
}
-Adam
Adam S. wrote:
This sounded like an interesting challenge - here’s a simple interface
to the Windows Media Player:
Wow !!! this is much more that I was expected ! Thanks millions for the
clear OLE usage example.
basic usage:
player = SimpleWinMediaPlayer.new
player.file = “c:/whatever.mp3”
player.play
In my case, adding this snippet directly to my code does not seem to
kick anything like a WMP… It seems that player is correctly
initialized (I can do 'ole_methods 'and ‘name’). ‘@control.playItem
@control.currentItem’ is executed but does seem to succesfully launch a
play. Any idea ?
Also, is there anything special about program termination ? Will the OLE
object survive ruby instance termination ? What should I write as a
waiting loop ? Any thread management needed ?
Thanks again,
Jb