Play MP3 audio files in rails

By , June 18, 2009 11:43 pm

If  you want to play mp3 files from your web application then you can do it by using SoundManager2 plugin.

There are few steps that you need to follow.

Step 1: Install the soundmanger2 plugin.

ruby script/plugin install http://soundmanager2.rubyforge.org/svn/soundmanager2

Step 2:  Generate the soundmanager2 in your app. This will store the necessary files in your app.

ruby script/generate sound_manager2

Above command give following result.

public/soundmanager2.swf
public/javascripts/soundmanager2.js
public/javascripts/soundmanager2-jsmin.js
public/javascripts/soundmanager2-rails.js
public/images/SoundManager2/pause-control.gif
public/images/SoundManager2/play-control.gif

Step 3: Add below code in your view file.

<%=javascript_include_tag :defaults%>
<%=sounds=[[sound1,"<MP3 FILE URL>"][sound2,"<MP3 FILE URL>"]]%>

<%=initialize_sounds(sounds) %>

Play sound 1: <%= toggle_sound "sound1" %>
Play sound 2: <%= toggle_sound "sound2" %>

Now run view file and you will get Start and stop button to play mp3 file.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

Leave a Reply

*

Panorama Theme by Themocracy