My django views.py
generates an mp3 file on the server at ./speech.mp3
after user interaction. How do I play it on the user's browser? If I just play the mp3 file using python code in views.py
, it'll only be played on the server PC, not the user's browser.
I'm thinking of 2 solutions:
Either I have to pass the mp3 to the user's browser through ajax
OR
upload it to a cloud service.
Not sure how to approach though.
index.js:
$.ajax({
url: "/text_speech/",
type: "POST",
data: {text: $("#id_text").val(),
voice: $("#id_voice").val(),
speed: speed.slider("option", "value")},
dataType: "json",
success: function (response){
},
error: function(xhr,errmsg,err) {
}
});
views.py:
class text_speech(base.TemplateView):
.....
@staticmethod
def post(request, *args, **kwargs):
form = forms.input_form(request.POST)
if form.is_valid():
# process the data
.....
# Generate the mp3 file at ./speech.mp3
amazon_polly.amazon_polly(....)
return http.JsonResponse({}, status=200)
else:
return http.JsonResponse({}, status=400)