python播放url音频,如何通过django(python)中的http响应播放音频文件
I want to make request to urland django view should read the file and send the http response back to play the same file in browser.I got the following code but it does't play anything please anyone ..
I want to make request to url and django view should read the file and send the http response back to play the same file in browser.I got the following code but it does't play anything please anyone help me..
Right now i hard coded the file name in the code.
url: http://localhost/playfile/audiofile_name
def playAudioFile(request):
try:
fname="C:\\test\\audio\\t.mp3"
wrapper = FileWrapper(file(fname))
print content_type
response = HttpResponse(wrapper, content_type="audio/mpeg")
print response
response['Content-Length'] =os.path.getsize(fname )
return response
except:
return HttpResponse()
Thank in advance..
解决方案
I found the answer.....
def playAudioFile(request):
fname="C:\\test\\audio\\audio.mp3"
f = open(fname,"rb")
response = HttpResponse()
response.write(f.read())
response['Content-Type'] ='audio/mp3'
response['Content-Length'] =os.path.getsize(fname )
return response
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)