使用django串流mp3文件,从< audio> [英] Streaming mp3 files with django, read from a page with <audio>
问题描述
我正在尝试使用一个小型应用程序来流式传输我的mp3文件与一个Web界面,我想到玩,并在python与django做服务器端。
I'm trying to make a small app to stream my mp3 files with a web interface, and I thought about playing with and do the server side in python with django.
我想要像/ stream / ID这样的网址来流传输与该ID相对应的mp3。
我在django中看到使用不同的方法为这些文件提供服务,最后一个我尝试的是一个描述这里
I want to have urls like /stream/ID to stream the mp3 corresponding to that ID. I made a view in django to serve these files using different methods, the last one I tried being the one described here
如果我从firefox访问/ stream / ID,直接用firefox-totem或一些插件播放mp3。如果我使用带有与源代码相同的URL的音频播放器的页面,它根本不起作用(使用通过apache提供的mp3文件的链接)。
If I access /stream/ID from firefox, it plays the mp3 directly with firefox-totem or some plugin like that. If I use the page with my audio player with the same url as the source it doesn't work at all (works with a link to an mp3 file served by apache).
这里是我的视图的代码(只发送一个测试文件)
here is the code of my view (only send one test file)
def stream(request):
resp = HttpResponse(FileIterWrapper(open('/.../test.mp3',"rb")),mimetype='audio/mpeg')
resp['Content-Length'] = os.path.getsize("/.../test.mp3")
resp['Content-Disposition'] = 'filename=test.mp3'
return resp
我切割完整路径,这不是问题。
I cut the full path, it is not the problem.
当查看django runserver输出时,我注意到每次音频播放器尝试,我得到这两个错误,
When looking at the django runserver output, I noticed that each time the audio player tries, I get these 2 errors,
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 281, in run
self.finish_response()
File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 321, in finish_response
self.write(data)
File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 417, in write
self._write(data)
File "/usr/lib/python2.6/socket.py", line 318, in write
self.flush()
File "/usr/lib/python2.6/socket.py", line 297, in flush
self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 104] Connection reset by peer
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 42891)
Traceback (most recent call last):
File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/pymodules/python2.6/django/core/servers/basehttp.py", line 562, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/lib/python2.6/SocketServer.py", line 618, in __init__
self.finish()
File "/usr/lib/python2.6/SocketServer.py", line 661, in finish
self.wfile.flush()
File "/usr/lib/python2.6/socket.py", line 297, in flush
self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 32] Broken pipe
当我直接访问流网址时,没有问题/错误。
no problem/error when I acces the stream URL directly.
我在chrome(最新版本,ffmpeg -extra安装),它可以正常从apache的mp3,但超时使用流网址。
I tried the same in chromium (latest version, with ffmpeg-extra installed), it works fine with the mp3 from apache, but timeouts when using the stream url.
我尝试在响应中设置不同的标头,但没有成功。
我正在设置内容长度,内容类型和内容配置
I tried setting different headers in the response, but with no success. I'm currently setting the content-length, content-type and content-disposition
我正在寻找新的想法尝试。
I'm looking for new ideas to try.
感谢您的帮助!
推荐答案
您可以尝试使用 wireshark ,以便在正常工作时观察您的浏览器/媒体播放器/ apache。
You could try using wireshark to observe your browser/media player/apache while it works correctly.
这篇关于使用django串流mp3文件,从< audio>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!