reactjs - 我需要从手机麦克风获取音频流并在 linux 中播放
问题描述
我尝试使用 react-native-recording 和 react-native-microphone-stream 包,但它们都返回 16 位整数。
componentDidMount() {
Recording.init({
bufferSize: 4096,
sampleRate: 44100,
bitsPerChannel: 16,
channelsPerFrame: 1,
})
const socket = openSocket('http://192.168.1.147:3000');
const listener = Recording.addRecordingEventListener(data => {
if (this.webView) {
this.webView.postMessage(data)
socket.emit("audio", data);
console.log(data);
}
})
Recording.start()
}
如何在我的 Linux 桌面中将返回的数字作为音频播放。我知道这里使用的是 PCM 格式。
解决方案
我已在服务器中发送了一个 16 位数组并将其保存在文件中
var io = require('socket.io')();
const fs = require('fs');
var spawn = require('child_process').spawn;
const port = 3000;
io.listen(port);
console.log("listening port 3000 ...");
io.on('connection', (client) => {
console.log('any value');
client.on('createConnection', () => {
client.emit('connectionResponse', 'New Client with ID: ' + client.id);
});
client.on('connection', (data) => {
console.log("client connected");
})
client.on('audio', (data) => {
// phone audio data parameters
//Recording.init({
//bufferSize: 256,
//sampleRate: 8000,
//bitsPerChannel: 16,
//channelsPerFrame: 1,
// play created out.pcm file with this command in linux machine
// aplay -r 8000 -t raw -f S16_LE out.pcm
console.log("data recivied");
for(let i=0 ; i < data.length; i++ ){
var buf = new Buffer(2);
buf.writeInt16LE(data[i],0);
console.log(buf);
fs.appendFile('out.pcm',buf, (err) => {
if (err) throw err;
//console.log('xThe lyrics were updated!');
})
}
});
})
可以在 Linux 机器上使用该命令播放创建的 out.pcm 文件
aplay -r 8000 -t raw -f S16_LE out.pcm
推荐阅读
- javascript - 在 FullCalendar 中格式化列标题日期
- android - 如何将外部 android 设备连接到 android studio - 注意,该设备不是手机/平板电脑
- html - 使用其他悬停效果将图像从灰度更改为彩色
- node.js - 为什么我无法从 api 得到正确的响应?
- python - 迁移学习 - 如何仅更改 TensorFlow 中的输出层?
- inno-setup - InnoTools Downloader 失败并显示“抱歉,无法下载文件”
- javascript - 函数语法js前的括号含义
- javascript - 如何在角度环境中使用来自 fullcalendar 的控件(特别是 eventAllow)来限制某些事件的拖放
- multipartform-data - 如何设置 Azure API 管理以接受带有表单数据字段的 POST 并将它们传递给旧 API
- c# - 使用 Async 调用 WebAPI 无法正确返回