I'm trying to write small website that can stream audio online(radio station) and got few questions: 1. Do i have to index all my music files into database, or i can randomily pick file from file system and play it. 2. When should i use ajax to load new song(right after last finished, or few seconds before to get responce from server with link to file?) 3. Is it worth to use ajax, or better make list, that will play its full time and then start over?
2 Answers
I think you are asking the wrong Questions.
- You need to think about how you will play the Audio in the browser (what player will you be using?).
- You don't need Ruby on Rails to deliver the Files to the client. They can be requested directly from the Web server (Apache or Nginx)
- Rails is only required for rendering the Website alongside the Player that will then play the Audio. Depending on what player you are using you can either control it through javascript to request the next song from the server or simply write a static playlist to the client that then gets played.
-
Most of all i'll use some simple js player + html5 audio api. But i'm not sure if i understood you correctly about requesting files using Web server, can you explain more details, or point me where i can read about this. I found some info on how to make radio streaming using php, and there are alot of code that doesnt response for views. Also i'd like to show current song name using ruby tag gem, that can read id3 tags from files– AvdeptCommented Sep 12, 2012 at 21:39
-
Well .. Ruby on Rails is a web framework that is focused on rendering HTML to the Client. Web servers main purpose (like Apache or NginX) is to serve Files from the Server's file-system to the Clients. So if you just put the mp3 file in the Rails application's public folder it is accessible from a client without you having to do anything..– TigraineCommented Sep 12, 2012 at 21:44
-
Well i think i wont do that, because ill be in need of huge space on hosting. Is it possible to use some cloud based system like dropbox or g-drive\skydrive to host audio files and then use them using RoR?– AvdeptCommented Sep 12, 2012 at 21:49
-
How you store the data is totally up to you. You could even go ahead and use SoundCloud to host the Data and only embed their Audio Player in your Web-Site.. Rails is only serving the Website that acts as a Directory to access the Audio sources. Where you store it is dependent on you... But webhosting for large volumes of audio files is not cheap.. Free services only take you so far..– TigraineCommented Sep 12, 2012 at 22:27
Regarding the streaming I aggree with @Tigraine
Now,
Single song play:
1 if you want to play an individual song then in your ajax call to action return a key value pair in json response which contains the audio URL of requested song.
For Playlist:
2. You should return an array which contains the audio URL in the same arrangement as they were added into playlist.
Then in your java script declare variable
var current_song
var total_song
In js audio player we commonly have methods which detects when the song completes and then on song complete you can increment the song counter as
current_song+=0
when the counter reaches to end reset it to one:
Sample code is:
Here I am using Jplayer you can use any other player which one of the best and fully customizable player.
var current_song = 0;
var total_songs;
var song_id_array;
$.ajax({
url:"<%=url_for :controller => 'cont_x',:action => 'song_in_playlist'%>",
data:'playlist_id=' + encodeURIComponent(playlist_id),
cache:false,
success:function (data) {
song_id_array = data.array; //data.array variable contain's the audio URL's array of songs inside selected playlist which is returned by the action //
total_songs = song_id_array.length;
}
})
current_song = 0;
play_right_song(current_song);
}
function play_right_song(current_song) {
$("#jquery_jplayer_1").jPlayer("destroy");
var audio_url_inside = song_id_array[current_song];
$('#jquery_jplayer_1').jPlayer({
ready:function (event) {
$(this).jPlayer("setMedia", {
mp3:audio_url_inside
}).jPlayer("play");
},
ended:function () { // The $.jPlayer.event.ended event
current_song += 1;
if (current_song >= total_songs) {
current_song = 0;
}
play_right_song(current_song);
},
swfPath:"<%= asset_path('Jplayer.swf')%>",
supplied:"mp3"
});
}
and if you want to handle next and previous song in playlist just ask for the code. I will update the answer.
-
Thanks for answer. But i understood that i need streaming server to broadcast music like on radio.– AvdeptCommented Oct 5, 2012 at 8:42