2017-02-21 18 views
1

我以前的代碼可以在我的設備上正常工作,但現在它根本不起作用。設備是三星SM-G900P(Android 6.0.2,API23)。當開始使用Watson STT錄製語音時,Android應用程序崩潰java-sdk

由於我的應用程序的其他部分需要更新版本的OkHttp庫,我不得不自己構建語音到文本,核心和文本到語音庫。我從PR#557建成。

這是我在運行Android Studio時遇到的錯誤。

E/AudioRecord: AudioFlinger could not create record track, status: -1 
E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 
E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 

E/AndroidRuntime: FATAL EXCEPTION: Thread-5952 
       java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. 
        at android.media.AudioRecord.startRecording(AudioRecord.java:943) 
        at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneCaptureThread.run(MicrophoneCaptureThread.java:63) 

一些代碼...

private void startListening() { 

    if (listening != true) { 
     capture = new MicrophoneInputStream(true); 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        speechService.recognizeUsingWebSocket(capture, getRecognizeOptions(), new MicrophoneRecognizeDelegate()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 
     listening = true; 
    } else { 
     try { 
      capture.close(); 
      listening = false; 
      //recordInstance.release(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

private class MicrophoneRecognizeDelegate implements RecognizeCallback { 

    @Override 
    public void onTranscription(SpeechResults speechResults) { 
     System.out.println(speechResults); 
     String text = speechResults.getResults().get(0).getAlternatives().get(0).getTranscript(); 
     showMicText(text); 
     if (speechResults.getResults().get(0).isFinal()) { 
      try { 
       capture.close(); 
       listening = false; 
       send(speechResults.getResults().get(0).getAlternatives().get(0).getTranscript()); 
      } catch (Exception e) { 
       System.out.println("error when closing capture"); 
       e.printStackTrace(); 
      } 
     } 

    } 

    @Override public void onConnected() { 
     System.out.println("mic connected"); 
    } 

    @Override public void onError(Exception e) { 
     e.printStackTrace(); 
    } 

    @Override public void onDisconnected() { 
     System.out.println("mic disconnected"); 
    } 
} 

感謝

回答

3

看起來像你對我沒有適當的權限進行錄音;通常這就是AudioFlinger could not create record track, status: -1表示的。從API 23開始,您需要在應用運行時請求某些權限;錄音屬於該設定內。退房https://developer.android.com/training/permissions/requesting.html

+0

謝謝。我的確有權限添加到應用程序,但沒有提示編碼問用戶的權限,所以麥克風的權限已關閉。我想我需要爲麥克風編碼提示,但我認爲它會自動提示。 – troyd

相關問題