Sergey
Sergey

Reputation: 21251

Django: how to give mp3 file correctly

The problem is I can't change playing position by clicking on a timeline in google Chrome (it always plays from start to end)
If Nginx gives mp3 file to the client everything is OK and I can change playing position.


In my script I give mp3 this way:

from django.core.servers.basehttp import FileWrapper
wrapper = FileWrapper(file( 'mp3file.mp3' ))
response = HttpResponse(wrapper, content_type='audio/mpeg')
response['Content-Length'] = os.path.getsize( 'mp3file.mp3' )
return response

The url is: http://server/mp3/###.mp3

So the whole file is given to the client, but still playing pos can't be changed. What is wrong?

PS: Do not use any proprietary sh*t like mp3 - use ".ogg" format

Upvotes: 1

Views: 2837

Answers (1)

BFil
BFil

Reputation: 13106

This is because the headers should handle additiona headers (like Accept-Ranges), and it should handle partial file requests

Doing this kind of things inside Django itself is a mess (I tried it some time ago), but then I ended up using Apache for serving files (this way you just don't waste resources)

You can consider using mod_xsendfile for being able to serve files from your django view using apache, in this way for example:

response = HttpResponse(mimetype='audio/mpeg')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['Accept-Ranges'] = 'bytes'
response['X-Sendfile'] = smart_str(path_to_file)
return response

Upvotes: 5

Related Questions