3

我正在 RPi3 上开发 Android Things SDK 的 Preview 2。尝试了录音机和媒体录音机,仍然无法捕获音频。我正在尝试将我的语音转换为文本。不支持常规语音识别。我将 USB MIC 直接连接到 RPi3 以及通过 USB 声卡连接到 RPi3 的耳机 MIC。

媒体记录器代码:

private void startRecording() {
    Log.d(TAG, "startRecording....");
    mRecorder = new MediaRecorder();
    Log.d(TAG, "startRecording: Audio Source"+MediaRecorder.getAudioSourceMax());
    mRecorder.setAudioSource(MediaRecorder.AudioSource.UNPROCESSED);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording() {
    // stops the recording activity
    if (mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
}

清单权限:

 <uses-permission android:name="android.permission.RECORD_AUDIO" />

错误:

03-09 17:17:38.662  3970  3970 D MainActivity: onCreate
03-09 17:17:38.668  3970  3970 D MainActivity: startRecording....
03-09 17:17:38.672  3970  3970 D MainActivity: startRecording: Audio Source9
03-09 17:17:38.678   161   161 E AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x1 channelMask 10
03-09 17:17:38.678   161   161 E AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x1, channelMask 0x10; status -22
03-09 17:17:38.678   161   161 E StagefrightRecorder: audio source is not initialized
03-09 17:17:38.678  3970  3970 E MediaRecorder: start failed: -2147483648
03-09 17:17:38.680  3970  3970 D AndroidRuntime: Shutting down VM
03-09 17:17:38.683  3970  3970 E AndroidRuntime: FATAL EXCEPTION: main
03-09 17:17:38.683  3970  3970 E AndroidRuntime: Process: com.example.androidthings.myproject, PID: 3970
03-09 17:17:38.683  3970  3970 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidthings.myproject/com.example.androidthings.myproject.MainActivity}: java.lang.RuntimeException: start failed.
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(ActivityThread.java)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6077)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-09 17:17:38.683  3970  3970 E AndroidRuntime: Caused by: java.lang.RuntimeException: start failed.
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.media.MediaRecorder.start(Native Method)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at com.example.androidthings.myproject.MainActivity.startRecording(MainActivity.java:181)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at com.example.androidthings.myproject.MainActivity.onCreate(MainActivity.java:63)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:6662)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
03-09 17:17:38.683  3970  3970 E AndroidRuntime:        ... 9 more
4

3 回答 3

2

我将从简单的部分开始。由于当前预览版中不提供语音识别服务,您可能需要查看在应用程序中处理语音到文本的替代方法。这是一篇博文,可以为您指出一些选项。

关于录音,这里有几个想法。AudioSource.MIC相反,在您的代码中使用可能会更好。我没有MediaRecorder直接测试过,但另一个建议是AudioRecord直接使用。特别是因为您的目标是将音频数据传递给另一个服务进行处理(可能不仅仅是将其保存到文件中)。这将使您能够使用每个采样的音频缓冲区。下面是一个应该在 Android Things 设备上工作以初始化录音的示例:

// Audio recording parameters
private static final int SAMPLE_RATE = 44100;
private static final int ENCODING_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private static final int CHANNEL_FORMAT = AudioFormat.CHANNEL_IN_MONO;

private AudioRecord mRecorder;
private final int mBufferSize = AudioRecord
        .getMinBufferSize(SAMPLE_RATE, CHANNEL_FORMAT, ENCODING_FORMAT);

public void initAudioRecorder() {
    if (mRecorder == null) {
        try {
            mRecorder = new AudioRecord.Builder()
                    .setAudioSource(MediaRecorder.AudioSource.MIC)
                    .setAudioFormat(new AudioFormat.Builder()
                            .setEncoding(ENCODING_FORMAT)
                            .setSampleRate(SAMPLE_RATE)
                            .setChannelMask(CHANNEL_FORMAT)
                            .build())
                    .setBufferSizeInBytes(2*mBufferSize)
                    .build();
            mRecorder.startRecording();
        } catch (UnsupportedOperationException e) {
            Log.w(TAG, "Unable to initialize recording", e);
        }
    }
}
于 2017-03-09T23:08:29.657 回答
0

每个设备可能有不同的初始化设置,因此您必须创建一个循环所有可能的比特率组合的方法,编码......:

private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    Log.d(C.TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e(C.TAG, rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}

AudioRecord recorder = findAudioRecord();
recorder.release();
于 2017-03-09T17:28:01.317 回答
0

我已经使用 RPi 3 / Things Preview 2 / USB mic / Kõnele ( https://github.com/Kaljurand/K6nele ) 进行语音识别,换句话说:Things Preview 2 确实支持录音和 SpeechRecognition (我假设你意思是SpeechRecognizer)。

Kõnele 开箱即用地认出了爱沙尼亚语。如果您需要其他语言,则需要更改 Kõnele 首选项中的服务器 URL(或使用“ee.ioc.phon.android.extra.SERVER_URL”EXTRA 覆盖它)并在此 URL 设置识别服务器。此处描述了启动服务器的最简单方法:https ://github.com/jcsilva/docker-kaldi-gstreamer-server

于 2017-03-15T22:00:44.703 回答