How to play a audio file through http response in

2020-06-29 09:43发布

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..

2条回答
Anthone
2楼-- · 2020-06-29 10:30

well if you have the file you can do this

s = Sound() 
s.read('sound.wav') 
s.play()
查看更多
劳资没心,怎么记你
3楼-- · 2020-06-29 10:41

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
查看更多
登录 后发表回答