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
3.9k views
in Technique[技术] by (71.8m points)

audio - 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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

1.4m articles

1.4m replys

5 comments

57.0k users

...