【问题标题】:How to play a microphone stream in React Native?如何在 React Native 中播放麦克风流?
【发布时间】:2021-04-27 11:56:52
【问题描述】:

目标是通过 Wifi Direct 在 React Native 中制作一个简单的对讲机。

我能够使用库 react-native-udp 捕获第一部手机 (192.168.49.1) 上的麦克风流并将其发送到第二部手机 (192.168.49.200)。

如何在另一部手机上播放传输的音频? 尝试了不同的音频应用程序,但它们不支持原始音频缓冲区 AFAIK? 我应该先将其保存到文件中,然后流式传输此本地文件吗? 有什么想法吗?

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;

【问题讨论】:

    标签: react-native audio stream udp playback


    【解决方案1】:

    你试过这个模块吗?这可能适合您的需要:

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

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

    但我不确定 iOS。

    或者您也可以自己编写原生模块并在 android 中使用 AudioTrack 类和在 iOS 中使用 AVAudioPCMBuffers 进行共享。

    一些有用的参考资料:

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2018-06-18
      • 2021-06-27
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多