目标是通过 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
但是我不确定iOS。
或者您可以自己编写 native 模块,并在 Android 中使用 AudioTrack
类和 iOS 中的 AVAudioPCMBuffers
进行共享。
一些有用的引用:
关于react-native - 如何在 React Native 中播放麦克风流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65854942/