How to Voice call using socket and node.js in Android

Viewed 27

Hello, I want to design a two-way telephone connection using socket.io in Android and socket in node.js, which takes the sound from the source phone and sends it to the destination phone in runtime, and instantly sends the sound from the source and sends bytes to the destination. Convert the transmission to audio and play the audio ! , The voice is sent to the server, but the server does not send the information to the destination!

public class VoiceCallActivity extends AppCompatActivity {
    private static final String LOG_TAG = "socket_main_act";
    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
    private static final int SAMPLING_RATE = 22050;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private MediaRecorder recorder = new MediaRecorder();
    private boolean permissionToRecord = false;
    private String[] permissions = {Manifest.permission.RECORD_AUDIO};
    private Socket mSocket;

    private void startRecording(Socket socket) {
        Log.i(LOG_TAG, "start recording.");
        try {
            ParcelFileDescriptor[] descriptors = new ParcelFileDescriptor[0];
            try {
                descriptors = ParcelFileDescriptor.createPipe();
            } catch (IOException e) {
                e.printStackTrace();
            }
            ParcelFileDescriptor recorderRead = new ParcelFileDescriptor(descriptors[0]);
            ParcelFileDescriptor recorderWrite = new ParcelFileDescriptor(descriptors[1]);
            InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(recorderRead);
            Log.i(LOG_TAG, "Setup IO.");

            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC_ELD);
            recorder.setOutputFile(recorderWrite.getFileDescriptor());
            recorder.setAudioSamplingRate(SAMPLING_RATE);
            Log.i(LOG_TAG, "Setup recorder.");
            recorder.prepare();
            recorder.start();
            Log.i(LOG_TAG, "Start recording.");

            int read = 1;
            byte[] data = new byte[SAMPLING_RATE];

            while (read != -1) {
                read = inputStream.read(data, 0, data.length);
                JSONObject obj = new JSONObject();
                try {
                    obj.put("data", 1);
                    socket.emit("audio_give", obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.i(LOG_TAG, "Data from recorder exhausted.");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "Recording faults");
        }
    }

    public Emitter.Listener onAudioBroadcast = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    byte[] data = Base64.decode(args[0].toString(), Base64.DEFAULT);
                    Log.e("TAG", "call: args = " + args[0]);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    ObjectOutputStream os;
                    try {
                   /* os = new ObjectOutputStream(out);
                    for (Object arg : args) {
                        os.writeObject(arg);
                    }
                    data = out.toByteArray();*/
                        Log.i(LOG_TAG, "Read data as byte array.");
                        AudioDataSource src = new AudioDataSource(data);
                        Log.i(LOG_TAG, "Setup player and src");
                        AudioAttributes attr = new AudioAttributes.Builder()
                                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                                .build();
                        Log.i(LOG_TAG, "Setup attributes");
                        mediaPlayer.setAudioAttributes(attr);
                        Log.i(LOG_TAG, "Player set with attributes.");
                        mediaPlayer.setDataSource(src);
                        Log.i(LOG_TAG, "Player read from source");
                        mediaPlayer.prepare();
                        Log.i(LOG_TAG, "prepare player.");
                        mediaPlayer.start();
                        Log.i(LOG_TAG, "Start playing.");
                    } catch (IOException e) {
                        Log.e(LOG_TAG, "Player failed to stream audio: " + e.toString());
                        e.printStackTrace();
                    }
                }
            });
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecord = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (!permissionToRecord) finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
        setContentView(R.layout.activity_main);
        try {
            IO.Options opts = new IO.Options();
            opts.reconnection = true;
            opts.port = 3000;

            opts.timeout = 15000;
            opts.reconnectionDelay = 10000;
            mSocket = IO.socket("http://192.168.2.1:3000", opts);
            mSocket.on("audio_receiver", onAudioBroadcast);

            mSocket.on(Socket.EVENT_CONNECT, onConnect);
            mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
            mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
            mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);

            mSocket.connect();
            startRecording(mSocket);
        } catch (URISyntaxException e) {
            Log.d(LOG_TAG, "uri is invalid.");
        }

    }

    Emitter.Listener onConnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("TAG", "run: onConnect");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        }
                    });
                }
            });
        }
    };

    Emitter.Listener onDisconnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("TAG", "run: onDisconnect");
                }
            });
        }
    };

    Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("TAG", "run: onConnectError");
                }
            });
        }
    };

}

server socket.io node.js code :

const express = require('express');
  const app = express();

  let http = require('http').Server(app);
  let io = require('socket.io')(http);

  http.listen(3000, function () {
      // Now listening
      console.log("listening 3000 !");
  });

  io.on('connection', function (socket) {
     socket.on('audio_give', function (data) {
        socket.emit("audio_receiver",{data})
     })
  })
0 Answers
Related