带有录音功能的YandexSpeechKit识别器

时间:2015-09-24 12:25:39

标签: android voice recording yandex

运行YandexSpeechKit识别器时,我可以同时录制文件中麦克风的声音吗?

需要同时进行语音识别(使用识别器类)并将设备麦克风的声音录制到文件中。使用标准机制MediaRecord是不可能的,因为MediaRecord和YandexSpeechKit使用本机方法和相同的资源。它导致某些进程(MediaRecord或Recognizer)的崩溃。

我正在尝试使用RecognizerListener - > onSoundDataRecorded(识别器识别器,byte []字节)代码如下:

@Override
public void onSoundDataRecorded(Recognizer recognizer, byte[] bytes) {

    Logger.d(TAG, "onSoundDataRecorded");
    write(bytes);
}     


public void write(byte[] bytes) {

    File file = getTmpFile();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file, true);
        fos.write(bytes);
    } catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        if(fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch(IOException e) {

            }
        }
    }
}

但是生成的文件无法播放。 有人能帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

Yandex SpeechKit返回原始PCM(16 kHz单声道16位)数据。你应该添加WAV标题或播放为PCM。例如,在类似unix的OS中通过sox:

play -r 16000 -b 16 -c 1 -e signed-integer filename.pcm

要添加WAV标头,您可以将此类https://github.com/MohammadAG/Android-SoundRecorder/blob/master/src/com/mohammadag/soundrecorder/WavConverter.java与参数

一起使用

private static final long SAMPLE_RATE = 16000; private static final int RECORDER_BPP = 16; private static final int CHANNELS = 1; private static final long BYTE_RATE = RECORDER_BPP * SAMPLE_RATE * CHANNELS/8;

        @Override
        public void onRecognizerRecordingBegin() {
            try {
                tempFileName = getFilename();
                os = new FileOutputStream(tempFileName, true);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onRecognizerRecordingDone() {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            int bufferSize = AudioRecord.getMinBufferSize(
                    16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            WavConverter.copyWaveFile(tempFileName, getFilename(), bufferSize);
            deleteTempFile();
        }

        @Override
        public void onRecognizerSoundDataRecorded(byte[] bytes) {
            try {
                os.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
相关问题