# Send a 30 Second Clip of an mp3

**URL:** <https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535>\
**Category:** Rails\
**Created:** [July 7, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535 "2006-07-07T19:17:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kortina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/977dab/32.png) [@kortina](https://www.ruby-forum.com/u/kortina)\
**Post date:** [July 7, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535/1 "2006-07-07T19:17:48Z")

</div>

On my site I’m allowing users to upload songs in mp3 format. Members  
can download the full song, but on public pages I would like to use a  
flash player to play a 30 second clip of the song and/or allow  
downloading of a 30 second mp3 snippet. The songs are stored above the  
web root directory, and I use the send\_file method for the full file  
downloads for members.

My question is, for the public view:

Is it possible to use the send\_file method to send just the first X  
seconds (or bytes) of a file?

If not, is there a way to crop the file somehow using a ruby script and  
save something equivalent to a ‘thumbnail’ like we use for images?

Also, if anyone has any suggestions for a flash player to use to stream  
mp3s I’d love to hear what they are.

---

<div class="post-metadata">

**Author:** ![kortina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/977dab/32.png) [@kortina](https://www.ruby-forum.com/u/kortina)\
**Post date:** [July 8, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535/2 "2006-07-08T18:41:15Z")

</div>

On 7/7/06, Andrew K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Also, if anyone has any suggestions for a flash player to use to stream  
> mp3s I’d love to hear what they are.

a while back I fiddled with xspf Web Music Player:

> **[XSPF Web Music Player](https://sourceforge.net/projects/musicplayer/)**
>
> Download XSPF Web Music Player for free. XSPF Web Music Player is a flash-based web application that uses xspf playlist format to play mp3 songs. XSPF is the XML Shareable Playlist Format, with specs available at http://www.xspf.org .

It seemed very nice and was easy enough to integrate with Rails. It may  
do  
just what you are looking for.

HTH,  
Howard

---

<div class="post-metadata">

**Author:** ![kortina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/977dab/32.png) [@kortina](https://www.ruby-forum.com/u/kortina)\
**Post date:** [February 4, 2007, 5:36am UTC](https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535/3 "2007-02-04T05:36:32Z")

</div>

Andrew K. wrote:

> On my site I’m allowing users to upload songs in mp3 format. Members  
> can download the full song, but on public pages I would like to use a  
> flash player to play a 30 second clip of the song and/or allow  
> downloading of a 30 second mp3 snippet. The songs are stored above the  
> web root directory, and I use the send\_file method for the full file  
> downloads for members.
> 
> My question is, for the public view:
> 
> Is it possible to use the send\_file method to send just the first X  
> seconds (or bytes) of a file?
> 
> If not, is there a way to crop the file somehow using a ruby script and  
> save something equivalent to a ‘thumbnail’ like we use for images?
> 
> Also, if anyone has any suggestions for a flash player to use to stream  
> mp3s I’d love to hear what they are.

Here’s what I did to solve this problem:

send the first 100KB of the song and then play it on the front end using  
a flash player:

```
path = "/tmp/my_song.mp3"
f = File.new(path, "r")
data = f.sysread(100 * 1000) # only play the first 100 KB of the song
send_data data, :disposition => "inline", :type => "audio/mpeg", 
:filename => "my_song.mp3"
```

This code goes in the controller of my rails app. If you don’t feed the  
URL of the controller method into a flash player, this will just make  
the first 100KB available as a file for download.

---

<div class="post-metadata">

**Author:** ![kortina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/977dab/32.png) [@kortina](https://www.ruby-forum.com/u/kortina)\
**Post date:** [February 4, 2007, 6:38am UTC](https://www.ruby-forum.com/t/send-a-30-second-clip-of-an-mp3/97535/4 "2007-02-04T06:38:52Z")

</div>

On 2/3/07, Andrew K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> data = f.sysread(100 \* 1000) # only play the first 100 KB of the song

A cosmetic suggestion: you can ditch the above comment if you change  
the line to:

data = f.sysread(100.kilobytes)

J.
