Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm making a few assumptions in this answer:</p> <ol> <li>You're using HTML5 markup</li> <li>You want a styled link "button" to trigger a sound</li> <li>You don't want to display the default HTML5 audio player</li> <li>The sound files are stored in <code>public/audios</code></li> <li>The sound file is pre-loaded on the page</li> </ol> <p>Unfortunately, the Rails 3.2 implementation of <code>audio_tag</code> is broken (<a href="https://github.com/rails/rails/issues/9373" rel="nofollow">https://github.com/rails/rails/issues/9373</a>). So unless you're using Rails 4, you're better off using the actual HTML markup.</p> <p>In this example, we're loading an mp3 file in the the default HTML5 audio player. The following snippet should be in your view file.</p> <pre><code>&lt;audio id="sound_effect" class="audio_player" controls="false" preload="true"&gt; &lt;source src="/audios/TrainWhistle.mp3" type="audio/mpeg"&gt; &lt;/audio&gt; </code></pre> <p>Since the HTML5 audio player comes with "built-in" display chrome, you may want to position it off the page with css. In this example, you would add the following to your css file:</p> <pre><code>.audio_player { position: absolute; top: -999em; { </code></pre> <p>Your link_to markup would look something like:</p> <pre><code>&lt;%= link_to 'At Station', '#', class: 'btn btn-success btn-medium', id: 'playsound' %&gt; </code></pre> <p>The javascript to actually play the sound will look similar to this jQuery example:</p> <pre><code>$('#playsound').on('click', function (e) { e.preventDefault(); $('#sound_effect').currentTime = 0; $('#sound_effect').play(); return false; }); </code></pre> <p>This example can easily be extended to support multiple files by small changes to the <code>link_to</code> tag and javascript.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload