我尝试使用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

关于reactjs - 我需要从手机麦克风获取音频流并在Linux中播放,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53551802/

10-10 10:31