I am thinking to build a custom view to handle the WebSocket, STOMP, AudioRecorder, AudioPlayer inside the Java directly instead of JS bridge -> Java, if you know how to create a custom View and integrate it with React Navigation, please let me know
My goal is to record audio data -> transmit it to a WebSocket endpoint.
Another device will receive the data from the WebSocket endpoint -> play the audio data. without the need to convert the audio data from byte[] -> WritableArray / String for recording audio, and for playing the audio from a WebSocket String -> byte[]
The current solution for the Audio Recorder:
- Record the audio data in byte[].
- Convert the audio data to WritableArray / String.
- Emit it to the JS Bridge.
- Send the audio data to the WebSocket endpoint.
The current solution for the Audio Player:
- Receive the audio data from a WebSocket endpoint.
- Run a ReactMethod with the audio data as the parameter, e.g
await AudioPlayerModule.streamAndLoadAsync(audioData)
.
@ReactMethod
public void streamAndPlayAsync(String data, final Promise promise) {
byte[] audioData = Base64.decode(data, Base64.NO_WRAP);
audioTrack.play();
audioTrack.write(audioData, 0, audioData.length);
audioTrack.stop();
promise.resolve(null);
}
- Convert the audio data to a byte[].
- Play the audio.
My expected result is the audio data played smoothly.
My actual result:
- 512 MB, virtual device, it sounds like the audio is played at 0.25 speed.
12-30 16:04:00.994 4740-4785/com.satpam I/ReactNativeJS: startAudioRecordingAsync()
12-30 16:04:01.264 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 363K, 42% free 6435K/10951K, paused 1ms+1ms, total 5ms
12-30 16:04:01.734 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 400K, 42% free 6429K/10951K, paused 12ms+12ms, total 29ms
12-30 16:04:02.174 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 380K, 42% free 6434K/10951K, paused 1ms+1ms, total 7ms
12-30 16:04:02.744 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 378K, 42% free 6447K/10951K, paused 12ms+12ms, total 30ms
12-30 16:04:03.194 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 396K, 42% free 6446K/10951K, paused 12ms+1ms, total 18ms
12-30 16:04:03.694 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 381K, 42% free 6458K/10951K, paused 11ms+1ms, total 20ms
12-30 16:04:04.214 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 387K, 42% free 6459K/10951K, paused 12ms+11ms, total 33ms
12-30 16:04:05.044 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 391K, 41% free 6482K/10951K, paused 14ms+21ms, total 127ms
12-30 16:04:05.384 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 408K, 41% free 6479K/10951K, paused 15ms+3ms, total 78ms
12-30 16:04:05.894 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 396K, 41% free 6495K/10951K, paused 12ms+5ms, total 29ms
12-30 16:04:06.284 4740-4785/com.satpam I/ReactNativeJS: stopAudioRecordingAsync()
12-30 16:04:06.804 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 395K, 41% free 6489K/10951K, paused 12ms+15ms, total 39ms
12-30 16:04:07.484 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 395K, 41% free 6496K/10951K, paused 12ms+14ms, total 35ms
12-30 16:04:07.924 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 403K, 41% free 6478K/10951K, paused 12ms+1ms, total 23ms
12-30 16:04:09.034 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 394K, 41% free 6474K/10951K, paused 14ms+4ms, total 31ms
12-30 16:04:09.854 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 391K, 41% free 6467K/10951K, paused 11ms+0ms, total 15ms
12-30 16:04:11.024 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 397K, 41% free 6462K/10951K, paused 13ms+14ms, total 45ms
12-30 16:04:12.824 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 408K, 42% free 6444K/10951K, paused 1ms+0ms, total 10ms
12-30 16:04:14.764 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 392K, 42% free 6439K/10951K, paused 13ms+12ms, total 35ms
12-30 16:04:16.774 4740-4744/com.satpam D/dalvikvm: GC_CONCURRENT freed 403K, 42% free 6432K/10951K, paused 17ms+20ms, total 67ms
- 6 GB, real device, the audio played smoothly.