質問

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?

役に立ちましたか?

解決

I usually just do

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

他のヒント

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

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

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top