'How to play the audio received as bytes over sockets in a flutter app?

I am building a flutter application that acts as a server. I have a python program in a computer which acts as a client. The client sends audio to the server in the form of bytes using sockets. The server(Flutter app) then should play the received audio bytes.

This is the client program in computer - Client.py

import socket
import threading, wave, pyaudio,pickle,struct

host_ip = '192.168.253.63'#  socket.gethostbyname(host_name)
port = 4444

def audio_stream():

    CHUNK = 1024
    wf = wave.open("temp.wav", 'rb')
    
    p = pyaudio.PyAudio()
    
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    input=True,
                    frames_per_buffer=CHUNK)

             

    #client_socket,addr = server_socket.accept()
    server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    server_socket.connect((host_ip,port))

    data = None 

    (nchannels,sampwidth,fs,nframes,_,_) = wf.getparams()

    readData = 0
    npackets = 1
    while True:
        data = wf.readframes(CHUNK)
        readData = readData + (CHUNK*sampwidth)
        a = pickle.dumps(data)
        message = struct.pack("Q",len(a))+a
        server_socket.sendall(message)
        if(readData>=sampwidth*nframes): break
        print(f"Packets sent: {npackets}")
        npackets = npackets+1
                
t1 = threading.Thread(target=audio_stream, args=())
t1.start()

This is the server part of code in my flutter app:

import 'dart:io';
import 'dart:typed_data';
import 'package:audioplayers/audioplayers.dart';

class Server {
  void initiate({required int port}) {
    print("Server Initiated...");
    ServerSocket.bind(InternetAddress.anyIPv4, port, shared: true)
        .then((ServerSocket server) {
      server.listen(_handleClient);
    });
  }

  AudioPlayer audioPlayer = AudioPlayer();

  playBytes(Uint8List byteData) async {
    int result = await audioPlayer.playBytes(byteData);
    print(result);
  }

  void _handleClient(Socket client) {
    print('Connection from '
        '${client.remoteAddress.address}:${client.remotePort}');
    client.listen((Uint8List data) async {
      //print(data);
      playBytes(data);
      print('Break');
    }, onError: (error) {
      print(error);
      client.close();
    }, onDone: () {
      print("Done");
      client.close();
    });
    //client.close();
  }
}

When I run the server and client, the client sends the audio bytes to server and the server is able to receive it. The problem lies in playing those bytes. I used audioplayers plugin for playing from bytedata. But it does not play and throws an error.

Can anyone tell what I did wrong, and what I need to do to play the audio byte data received from sockets in my flutter app?

I dont want to write the byte data into any files. I need to play the audio byte data as it is being received.

Thank you



Solution 1:[1]

are you running the flutter app on an IOS device? Please note it is working only on android. According to documentation " To play a file in the form of a data buffer (Uint8List), use the method playBytes. This currently only works for Android (requiring API >= 23, be sure to handle that if you use this method on your code)."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1