1

My problem is still no examples on how to transcribe audio file to text using android sdk of ibm watson speech to text and i see the method of audio file writter but there is no example on how to implement it or how to use it please help me i tried all the example of java sdk i try this

 SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");

InputStream audio = new FileInputStream("src/test/resources/sample1.wav");

RecognizeOptions options = new RecognizeOptions.Builder()
  .audio(audio)
  .contentType(HttpMediaType.AUDIO_WAV)
  .interimResults(true)
  .build();

service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
  @Override
  public void onTranscription(SpeechRecognitionResults speechResults) {
    System.out.println(speechResults);
  }
});

// wait 20 seconds for the asynchronous response
Thread.sleep(20000);

but still android force to stopped itry this

 SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");

File audio = new File("src/test/resources/sample1.wav");

RecognizeOptions options = new RecognizeOptions.Builder()
  .audio(audio)
  .contentType(HttpMediaType.AUDIO_WAV)
  .build();

SpeechRecognitionResults transcript = service.recognize(options).execute();
System.out.println(transcript);

but still android forced to stopped again please need your help anyone no examples for this android sdk :( please i posted 5 question but still no one help me please i need your help :( :(

This is the error that I encountered please help me

    Process: com.example.ezminute, PID: 28683
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
    at java.net.InetAddress.getAllByName(InetAddress.java:787)
    at okhttp3.Dns$1.lookup(Dns.java:40)
    at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
    at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
    at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at com.ibm.watson.developer_cloud.service.security.IamTokenManager.callIamApi(IamTokenManager.java:187)
    at com.ibm.watson.developer_cloud.service.security.IamTokenManager.requestToken(IamTokenManager.java:108)
    at com.ibm.watson.developer_cloud.service.security.IamTokenManager.getToken(IamTokenManager.java:78)
    at com.ibm.watson.developer_cloud.service.WatsonService.setAuthentication(WatsonService.java:375)
    at com.ibm.watson.developer_cloud.speech_to_text.v1.SpeechToText.recognizeUsingWebSocket(SpeechToText.java:391)
    at com.example.ezminute.activities.tryconvert$1.onClick(tryconvert.java:55)
    at android.view.View.performClick(View.java:6909)
    at android.widget.TextView.performClick(TextView.java:12693)
    at android.view.View$PerformClick.run(View.java:26199)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

1 Answers1

0

Based on the error stack, I think that this is the same problem as Android http connection exception and Error StrictMode$AndroidBlockGuardPolicy.onNetwork

Where in the device is throwing an exception whilst checking for StrictMode$AndroidBlockGuardPolicy

This appears to prevent your app from performing long running blocking operations in the main thread which would cause the device UI to hang.

Work arounds can be found in the provided links to prior questions. Though I think you should be spawning a thread to perform the operation, rather than overriding the strict mode policy.

chughts
  • 4,210
  • 2
  • 14
  • 27