Sharadhi Ballal
Sharadhi Ballal

Reputation: 663

How to play a audio file through http response in django(python)

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

Upvotes: 1

Views: 6139

Answers (2)

Sharadhi Ballal
Sharadhi Ballal

Reputation: 663

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

Upvotes: 11

user2369288
user2369288

Reputation: 1

well if you have the file you can do this

s = Sound() 
s.read('sound.wav') 
s.play()

Upvotes: -6

Related Questions