简体   繁体   中英

Streaming mp3 files with django, read from a page with <audio>

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.

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

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.

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.

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.

Thanks for your help!

您可以尝试使用Wireshark观察浏览器/媒体播放器/ apache正常运行的情况。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM