如何在Android Things上录制语音

时间:2017-03-09 17:00:17

标签: android speech-recognition android-things audio-capture

我正在研究RPi3上Android Things SDK的预览2。尝试过的录音机以及媒体录音机,仍然无法获取音频。我想把我的演讲转换成文字。不支持常规SpeechRecognition。我将USB MIC直接连接到RPi3以及通过USB声卡连接到RPi3的耳机MIC。

MediaRecorder代码:

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

3 个答案:

答案 0 :(得分:2)

我将从容易的部分开始。由于语音识别服务在当前预览中不可用,您可能希望查看在应用中处理语音到文本的替代方法。这是一个blog post,可以为您指出一些选项。

关于录音,这里有几个想法。您可能会在代码中使用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);
        }
    }
}

答案 1 :(得分:0)

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

\<

答案 2 :(得分:0)

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

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