Django:如何正确给出mp3文件
问题是我无法通过单击 Google Chrome 中的时间线来更改播放位置(它总是从头到尾播放)
如果Nginx向客户端提供mp3文件,一切都OK,我可以更改播放位置。
在我的脚本中,我这样给出mp3:
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
url 是: http://server/mp3/###.mp3
所以整个文件都给了客户端,但仍然播放 pos 不能被改变。怎么了?
附: 不要使用任何专有的东西,比如 mp3 - 使用“.ogg”格式
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为标头应该处理附加标头(如 Accept-Ranges),并且它应该处理部分文件请求
在 Django 本身内部做这种事情是一团糟(我前段时间尝试过),但后来我最终使用了 Apache用于提供文件(这样就不会浪费资源)
您可以考虑使用 mod_xsendfile使用 apache 从 django 视图中提供文件,例如:
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: