【问题标题】:SpeechToText IBM Watson ExceptionInInitializerErrorSpeechToText IBM Watson ExceptionInInitializerError
【发布时间】:2017-12-21 16:59:17
【问题描述】:

我在测试 IBM Watson SpeechToText Api 时遇到问题。

  SpeechToText service = new SpeechToText();
        service.setUsernameAndPassword("<UserName>", "<Password>");

        File audio = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                +MEDIA_FOLDER+"/myaudio.amr");

        SpeechResults transcript = service.recognize(audio).execute();
        System.out.println(transcript);

我在SpeechToText service = new SpeechToText(); 行收到java.lang.ExceptionInInitializerError 错误

错误

java.lang.ExceptionInInitializerError
at com.fexcon.voicetotext.MainActivity.processAudioIBM(MainActivity.java:190)
at com.fexcon.voicetotext.MainActivity$4.onClick(MainActivity.java:100)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'X'
at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:314)
at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:303)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:356)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:249)
at com.ibm.watson.developer_cloud.util.DateDeserializer.<init>(DateDeserializer.java:52)
at com.ibm.watson.developer_cloud.util.GsonSingleton.registerTypeAdapters(GsonSingleton.java:53)
at com.ibm.watson.developer_cloud.util.GsonSingleton.createGson(GsonSingleton.java:42)
at com.ibm.watson.developer_cloud.util.GsonSingleton.getGsonWithoutPrettyPrinting(GsonSingleton.java:76)
at com.ibm.watson.developer_cloud.speech_to_text.v1.SpeechToText.<clinit>(SpeechToText.java:119)
at com.fexcon.voicetotext.MainActivity.processAudioIBM(MainActivity.java:190) 
at com.fexcon.voicetotext.MainActivity$4.onClick(MainActivity.java:100) 
at android.view.View.performClick(View.java:5184) 
at android.view.View$PerformClick.run(View.java:20910) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

【问题讨论】:

  • 是 service.setUsernameAndPassword();你的自定义函数?

标签: java android ibm-cloud speech-to-text ibm-watson


【解决方案1】:

我认为你的 setUsernameAndPassword 是错误的。 它应该是:setCredentials(字符串用户名,字符串密码) 除非它是您的自定义函数。

在链接底部找到函数:setCredentials(String username, String password)

另外你需要初始化共享实例并使用它,

     /**
     * Init the shared instance with the context
     * @param uri
     * @param ctx
     * @param sc
     */
    public void initWithContext(URI uri, Context ctx, SpeechConfiguration sc){
        this.setHostURL(uri);
        this.appCtx = ctx;
        this.sConfig = sc;
    }

尝试按照快速入门指南中提供的此步骤操作。

private boolean initSTT() {
     // initialize the connection to the Watson STT service
     String username = getString(R.string.STTdefaultUsername);
     String password = getString(R.string.STTdefaultPassword);
     String tokenFactoryURL = getString(R.string.STTdefaultTokenFactory);
     String serviceURL = "wss://stream.watsonplatform.net/speech-to-text/api";
     SpeechConfiguration sConfig = new SpeechConfiguration(SpeechConfiguration.AUDIO_FORMAT_OGGOPUS);
     SpeechToText.sharedInstance().initWithContext(this.getHost(serviceURL), getActivity().getApplicationContext(), sConfig);
     // Basic Authentication
     SpeechToText.sharedInstance().setCredentials(username, password);
     SpeechToText.sharedInstance().setModel(getString(R.string.modelDefault));
     SpeechToText.sharedInstance().setDelegate(this);
     return true;
 }

【讨论】:

  • 第 1 行 SpeechToText service = new SpeechToText(); 没有问题。和这里提到的 setUsernameAndPassword 。 github.com/watson-developer-cloud/java-sdk/tree/develop/…
  • 可以尝试不使用“new speechtotext()”。而是使用 sharedinstance 和: SpeechToText.sharedInstance().setCredentials( "", "" )
  • setCredentials 显示我未解决。只有setUsernameAndPassword 有效。我的依赖有什么问题吗?我正在使用compile 'com.ibm.watson.developer_cloud:speech-to-text:3.8.0' compile 'com.ibm.watson.developer_cloud:java-sdk:3.7.2'
  • 我认为我们正在使用不同的库。我在这里使用一个:github.com/watson-developer-cloud/speech-android-sdk
  • 对。生病检查那个
【解决方案2】:

更新

它只发生在较低级别的 SDK 设备上。一旦我添加了最新的speech-to-text 库,它就解决了。 'com.ibm.watson.developer_cloud:speech-to-text:3.8.1-SNAPSHOT' 安装在'com.ibm.watson.developer_cloud:speech-to-text:3.8.0' Info.


今天我从头开始了另一个 android 项目,但没有得到上述运行时异常。我已经设法从波形文件中取出文本。

例子:

 final SpeechToText service = new SpeechToText();
            service.setUsernameAndPassword("<YourUsername>", "<Password>");

            final File audio = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/about_time.wav");

            final RecognizeOptions options = new RecognizeOptions.Builder()
                    .continuous(false)
                    .model("en-US_NarrowbandModel")
                    .interimResults(true)
                    .contentType(HttpMediaType.AUDIO_WAV)
                    .build();



            new Thread(new Runnable() {
                public void run() {
                    SpeechResults transcript = service.recognize(audio, options).execute();
                    System.out.println(transcript);
                }
            }).start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 2016-09-10
    • 2015-09-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多