Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

java - Is there a way to emit byte[] to React Native JS Bridge and vice versa?

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:

  1. Record the audio data in byte[].
  2. Convert the audio data to WritableArray / String.
  3. Emit it to the JS Bridge.
  4. Send the audio data to the WebSocket endpoint.

The current solution for the Audio Player:

  1. Receive the audio data from a WebSocket endpoint.
  2. 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);
  }
  1. Convert the audio data to a byte[].
  2. Play the audio.

My expected result is the audio data played smoothly.

My actual result:

  1. 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
  1. 6 GB, real device, the audio played smoothly.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...