2013-05-09 229 views
0

我想向url和django视图发出请求,应该读取该文件并发回http响应,以便在browser.I中播放相同的文件。但它不会播放任何东西,请任何人帮助我.. 现在我硬编码的代码中的文件名。如何在django(python)中通过http响应播放音频文件

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() 

预先感谢..

回答

4

我找到了答案.....

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

好,如果你有一个文件,你可以做到这一点

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

你确定django中有一个'Sound'类,它的行为与问题作者期望的一样? – Alexey 2013-05-10 09:11:16