2017-03-09 43 views
3

我正在研究RPi3上Android Things的SDK的預覽版2。嘗試錄音機以及媒體錄音機,仍然無法獲取音頻捕獲。我正在試圖將我的演講轉換爲文字。不支持常規SpeechRecognition。我有USB MIC直接連接到RPi3以及通過USB聲卡連接到RPi3的耳機MIC。如何在Android上錄製語音事物

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 

回答

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(); 
+0

這給所有導致NULL的組合導致了不良的緩衝區大小AudioRecorder –

2

我將從容易的部分開始。由於語音識別服務在當前預覽中不可用,因此您可能需要查看用於在應用中處理語音到文本的其他方法。這是一個blog post,可以指出你的一些選擇。

關於音頻記錄,這裏有一些想法。相反,在代碼中使用AudioSource.MIC可能會有更好的運氣。我還沒有直接測試MediaRecorder,但另一個建議是直接使用AudioRecord。特別是因爲你的目標是將音頻數據傳遞給另一個服務進行處理(可能不只是將其保存到一個文件中)。這將使您能夠使用每個採樣的音頻緩衝區。下面是應該在Android事情的設備上運行初始化錄音的東西的例子:

// 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); 
     } 
    } 
} 
+0

上面的代碼我已經嘗試過,它沒有奏效。然而,博客正是我想要的。但無論是內置語音API(案例#1)還是使用谷歌雲語音API(案例#2)。案例#1我已經嘗試過,但我錯過了我的設備中的基礎apk。對於案例#2,我需要首先捕獲音頻,這是我卡住的地方! –

0

我已經得到了語音識別與RPI 3個工作/物聯網預覽2/USB麥克風/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