Android Question Call google speech from service - SDK 30

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends, please for advice,

B4X:
   Dim i As Intent
   i.Initialize("android.speech.action.RECOGNIZE_SPEECH", "")

Call this voice recognitioning from APP (I see the app) - everything is OK

Call this voice recognitioning from service (app is in background) - NOK, I get:
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:1195)

EDIT, notice - Only for certain I must write - now I dont know what I am doing wrong, but after recognize speech I can not run audio recording from service (app in background). In sdk29 = OK, sdk30=NOK. I dont know if ins sdk30 is some new security. I am writing question because I hope that somebody have same situation.
EDIT2 - if I run recognizer service from service (app is background) I realy get error: "Insufficient permissions"

Do you think please it is because of SDK 30 - some security?
best regards
p4ppc
 
Last edited:

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear Mr. Erel,

thank you very much for your answer. It will take me some time to put here some code because I must take some parts from project.
I am sending small part. If I run from service (app is in background) this:
B4X:
Sub StartListening
    Dim i As Intent
    i.Initialize("android.speech.action.RECOGNIZE_SPEECH", "")  
    i.PutExtra("android.speech.extra.LANGUAGE", "IT")  
    sr.StartListening(sr.CreateRecognizeIntent2(i))  
    sr.StartListening(obj)  
End Sub

then I get this error:
"Insufficient permissions"

I have more troubles with SDK 30 (audio recording...), but I am isolating now only this situation which not exists in SDK29, but in SDK 30 yes.
Best regards
p4ppc
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
RECORD_AUDIO permission required. also speech recognition cannot run from background.
from https://developer.android.com/reference/android/speech/SpeechRecognizer :

This class provides access to the speech recognition service. This service allows access to the speech recognizer. Do not instantiate this class directly, instead, call SpeechRecognizer#createSpeechRecognizer(Context), or SpeechRecognizer#createOnDeviceSpeechRecognizer(Context). This class's methods must be invoked only from the main application thread.

The implementation of this API is likely to stream audio to remote servers to perform speech recognition. As such this API is not intended to be used for continuous recognition, which would consume a significant amount of battery and bandwidth.

Please note that the application must have Manifest.permission.RECORD_AUDIO permission to use this class.
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear Drgottjr,

thank you very much for your advice.

I have tested your words and its true - realy here is not solution how to start audiorecordning from service (app in background) ?
My results from testing show that after some time (half hour or an hour or 2 hour) the app crash when using record (record permission).


Best regards
p4ppc
 
Last edited:
Upvote 0
Top