Search code examples
javascriptruby-on-railsrubyruby-on-rails-5

Play MP3 in browser


I'm trying to allow a user on my site to click a button and via an ajax call load an mp3 file that is created on the server to their browser and then begin playing that file. The file cannot be publicly accessible sense it contains user account specific information. So, I've created the server side code that creates the mp3 but I do not know how to stream the file to the browser from my controller. Here is my current controller method.

def play_mp3
    t = TextToSpeech.new(current_user, "Play this text in the saved mp3 file")
    mp3_file = t.text_to_speech
end

Solution

  • <audio autoplay controls="controls">  
       <source src="/YOURMP3FILE.mp3" />  
    </audio>