I am using Django Rest Framework for Backend and Simple JavaScript, HTML, CSS for frontend.
I want to know and implement how to stream the audio file from backend to frontend so that user can play the audio in frontend. I don’t want to send whole data at once, I want to send piece by piece.
please explain me the concept of sending this streaming data also. I am kind of new to this.
I have tried StreamingHttpResponse from django.http but it’s kind of too abstract for me to understand the concepts of streaming and transfer of file other than just text from backend to frontend.
I am expecting ways to implement the streaming through concepts so that it can help me efficiently transfer the data in any language ( currently working with python django rest framework )
Here is my method of implementation of sending streaming data
class PlayView(APIView):
def get(self, request, song_id):
song = Songs.objects.filter(id=song_id).first()
try:
response = StreamingHttpResponse(file_iterator(song.song_file.path))
response[‘Content-Disposition’] = f’attachment; filename=”{song.name}.mp3″‘
response[‘Content-Type’] = ‘audio/mpeg’
return response
except Songs.DoesNotExist:
return Response({“error”: “Song not found”}, status=404)