Newbie question on music/windows

J

Jb Piacentino

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
 
A

Adam Shelly

This sounded like an interesting challenge - here's a simple interface
to the Windows Media Player:
---
# SimpleWinMediaPlayer.rb
# - Adam Shelly, January 2006
#
require "win32ole"
class SimpleWinMediaPlayer
def initialize
@mp =3D WIN32OLE.new("WMPlayer.ocx")
p @mp
@control =3D @mp.controls
end
def file=3D str
@mp.URL=3D str
@media =3D @mp.currentMedia
@control.currentItem =3D @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 =3D~ /(\d*):(\d+):)(\d*))?/
pos =3D $1.to_f*60+$2.to_f+$4.to_f/1000.0
end
@control.currentPosition =3D pos.to_f
end
def duration
@media.duration
end
end

----
basic usage:
player =3D SimpleWinMediaPlayer.new
player.file =3D "c:/whatever.mp3"
player.play

Hope this helps

-Adam
 
J

Jb Piacentino

Adam said:
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
 
A

Adam Shelly

------=_Part_6994_722166.1137437308599
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D SimpleWinMediaPlayer.new
w.volume =3D volume
songlist.each {|song|
puts (fullname =3D 'file://'+dirname+'/'+song)
w.file=3D fullname
sleep 0.1
w.play
sleep 0.1
puts w.duration
sleep (w.duration)
while w.current_pos !=3D ""
sleep (0.5)
end
}


-Adam
Wow !!! this is much more that I was expected ! Thanks millions for the
clear OLE usage example.

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

------=_Part_6994_722166.1137437308599--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top