Ulti
Ulti

Reputation: 21

How to play a microphone stream in React Native?

The goal is to make a simple walkie-talkie in React Native through Wifi Direct.

I was able to capture a microphone stream on the first phone (192.168.49.1) and send it to the second phone (192.168.49.200) by using the library react-native-udp.

How do I play the transferred audio on the other phone? Tried different audio apps, but they don't support raw audio buffer AFAIK? Should I first save it to a file and then stream this local file? Any ideas?

import React, {Component} from 'react';
import {ScrollView, StyleSheet, Text, View} from 'react-native';
import Recording from "react-native-recording";
import dgram from 'react-native-udp';

class App extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    Recording.init({
      bufferSize: 4096,
      sampleRate: 44100,
      bitsPerChannel: 16,
      channelsPerFrame: 1,
    });    

    Recording.start();

    let a = dgram.createSocket('udp4');
    let aPort = 23456;
    a.bind(aPort, '192.168.49.1', function(err) {
      if (err) throw err;
    });

    a.once('listening', function() {
      const listener = Recording.addRecordingEventListener((data) => {
        a.send(msg, undefined, undefined, aPort, '192.168.49.200', function(err) {
          if (err) throw err;
        });
      }
    }
  }

  render() {
    return (
      <View style={styles.container}>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }
}

export default App;

Upvotes: 2

Views: 1918

Answers (1)

Matin Zadeh Dolatabad
Matin Zadeh Dolatabad

Reputation: 1017

Did you try this module? This might suit your need:

https://github.com/hqwhuang/react-native-AudioBufferPlayer

Android: https://github.com/hqwhuang/react-native-AudioBufferPlayer/blob/d6fb7cf13850055d38de27fdedaa72e2d1507092/android/src/main/java/com/reactlibrary/RNReactNativeAudioBufferPlayerModule.java#L43

But I am not sure about iOS.

Or you can write the native module on your own and share using AudioTrack class in android and AVAudioPCMBuffers in iOS.

Some helpful references:

Upvotes: 2

Related Questions