react-native - 如何在 React Native 中播放麦克风流?

标签 react-native audio stream udp playback

目标是通过 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;

最佳答案

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

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。

或者您可以自己编写 native 模块,并在 Android 中使用 AudioTrack 类和 iOS 中的 AVAudioPCMBuffers 进行共享。

一些有用的引用:

关于react-native - 如何在 React Native 中播放麦克风流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65854942/

相关文章:

javascript - React Native 没有从 JS 错误中得到有效的回调

javascript - React native,添加 Root View 时出错

ios - 将带符号的 16 位 PCM 样本转换为无符号的 8 位 PCM 样本时进行裁剪

java - 为什么 AudioInputStream 读取永远不会返回 -1?

c++ - std::copy 从文件读取矩阵时出现问题

react-native - React Native 插值超出最大调用堆栈大小

android - 没有为类 RCTMap 定义 View 管理器

python - 音频识别和比较

c++ - 播放具有可变参数的正弦波 - 如何进行相移?

Django StreamingHttpResponse 到模板中