Over a million developers have joined DZone.

Share your music library on the network

DZone's Guide to

Share your music library on the network

· ·
Free Resource


#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)

require 'sinatra' # gem install sinatra

# specify port and environment as production
# to allow external access to server
set :port, 3000
set :environment, :production
# your media library path
MEDIA_PATH = '/Volumes/Data/Songs'

# get all mp3 files in my media library and sort
# then by file name
mp3s = Dir.glob("#{MEDIA_PATH}/**/*.mp3")
data = mp3s.map do |mp3|
    path: mp3,
    filename: File.basename(mp3, '.mp3')

# render the index page as set if files names
# and a player beside it, player will sent file index in data variable
# and another path will read file to stream it
get '/' do
  media_partial = data.map.with_index do |d, i| 
  end.join '
' <<-EOT Shared Media Center #{media_partial}
#{data.size} Media files found. EOT end # this path will catch any url starts with "play" # and will stream media to user, so when # user hit the play button it'll start playing # the mp3 file get '/play/:id' do send_file data[params[:id].to_i][:path] end

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}