Forum: Ruby Newbie question on music/windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
610cbcb3ff394be5ec86303bef340900?d=identicon&s=25 Jb Piacentino (jbpiacentino)
on 2006-01-12 10:37
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
B7f73e9f9d35be6a44b15f603f2910d4?d=identicon&s=25 Wybo Dekker (Guest)
on 2006-01-12 10:57
(Received via mailing list)
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/rd...
610cbcb3ff394be5ec86303bef340900?d=identicon&s=25 Jb Piacentino (jbpiacentino)
on 2006-01-12 11:24
Wybo Dekker wrote:
>
> you may be interested in
> http://localhost:8808/doc_root/ruby-mp3info-0.4/rd...

Yes I got this one, but this only deals with getting infos on the file -
does not help me with playing it !
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-01-12 22:52
(Received via mailing list)
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 = 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
610cbcb3ff394be5ec86303bef340900?d=identicon&s=25 Jb Piacentino (jbpiacentino)
on 2006-01-16 17:38
Adam Shelly 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
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-01-16 19:50
(Received via mailing list)
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
C7fa372a89015590968718a5198dd856?d=identicon&s=25 Jesus Morat (locutor99)
on 2016-03-16 19:32
You can download free royalty-free music to
http://www.locutortv.es/musicalibredederechos_music_free.htm .  It is
free.
This topic is locked and can not be replied to.