我正在编写一个Ruby on Rails应用程序,它允许用户上传一个mp3文件,然后播放它。我让它工作到用户可以做这些事情的程度,但是在寻找这首歌时存在问题。如果用户向前寻找(或让它播放)到歌曲中的某个位置,通常是通过歌曲的方式大约2/3或3/4,那么尝试回到开头(例如0:20),播放计时器将转到0:20,就像它应该,但实际音频将重新开始,就像用户寻求0:00一样。
现在我只是试图让这首歌在Chrome的基本html5 mp3播放器中播放,它在传递mp3文件时使用。这是我用来提供文件的代码,希望包含所有正确的标题:
file_begin = 0
file_size = @media.file_file_size
file_end = file_size - 1
if !request.headers["Range"]
status_code = "200 OK"
else
status_code = "206 Partial Content"
match = request.headers['range'].match(/bytes=(\d+)-(\d*)/)
if match
file_begin = match[1]
file_end = match[1] if match[2] && !match[2].empty?
end
response.header["Content-Range"] = "bytes " + file_begin.to_s + "-" + file_end.to_s + "/" + file_size.to_s
end
response.header["Content-Length"] = (file_end.to_i - file_begin.to_i + 1).to_s
response.header["Last-Modified"] = @media.file_updated_at.to_s
response.header["Cache-Control"] = "public, must-revalidate, max-age=0"
response.header["Pragma"] = "no-cache"
response.header["Accept-Ranges"]= "bytes"
response.header["Content-Transfer-Encoding"] = "binary"
send_file(DataAccess.getUserMusicDirectory(current_user.public_token) + @media.sub_path,
:filename => @media.file_file_name,
:type => @media.file_content_type,
:disposition => "inline",
:status => status_code,
:stream => 'true',
:buffer_size => 4096)
我很欣赏对此问题的任何见解。我觉得我很亲密,因为它几乎都有用,除了在开头附近寻找新的请求。
谢谢!
答案 0 :(得分:0)
重新发明轮子是一项要求吗?我问的原因是你可以简单地允许用户上传MP3文件并使用html5的功能,因为你的目标是Chrome。 Paperclip可以处理上传,到公共目录中的某个地方,你只需将该路径移交给the tag。
从我的评论(下方)中删除了疯狂的想法,以防止混淆其他SO读者。请参阅我上一篇关于解决方案的评论,但Amazon S3几乎是最佳解决方案。