Bhushan Lodha
Bhushan Lodha

Reputation: 6862

how do I play mp3 file?

How can I write a script in ruby which plays mp3 file (background-music) when executed from command-line?

I tried this

run = "mplayer #{"/Users/bhushan/resume/m.mp3"} -ao sdl -vo x11 -framedrop -cache 16384 -cache-min 20/100"
system(run)

but it is not working also, above is player specific. what if user don't have mplayer installed. Id there a better way?

Upvotes: 5

Views: 9807

Answers (4)

shevy
shevy

Reputation: 1000

You can write ruby code that uses different players.

For instance, a class I use:

https://gist.github.com/2217498

Rather than mplayer, one could use sox or vlc or something else.

In your example, you can always extend your code. Rather than hardcoded mplayer, you could read this from a yaml file, which you can dynamically change.

You can also try to use gstreamer via ruby-gtk directly.

But remember, ruby in itself can not play audio files.

It would be cool if someone would be able to create a pure ruby player though.

Upvotes: 1

Said Kaldybaev
Said Kaldybaev

Reputation: 9952

Try this way: this uses Shoes to do the magic, it's all you need i hope http://rubylearning.com/blog/2008/05/31/a-teeny-weeny-mp3-player-using-ruby-and-shoes/

#my_mp3player01.rb
Shoes.app do
  button( 'play' ){ @v.play }
  button( 'pause' ){ @v.pause }
  button( 'stop' ){ @v.stop }
  @v = video "C:/rubyprograms/mp3player/ruby.mp3"
end

Upvotes: 7

nex
nex

Reputation: 685

I usually just do

pid = fork{ exec 'mpg123','-q', file }

Upvotes: 15

rogerdpack
rogerdpack

Reputation: 66741

here's how I play them with jruby and an external jar: https://github.com/rdp/jruby-swing-helpers/blob/master/spec/play_mp3_audio.spec.rb

Upvotes: 1

Related Questions