androidspeech/README.md

78 строки
2.7 KiB
Markdown
Исходник Обычный вид История

2018-08-24 09:27:50 +03:00
# androidspeech
2018-08-29 09:39:24 +03:00
This is an Android library containing an API to Mozilla's speech recognition services.
## Installation
```
dependencies {
2019-02-04 19:57:56 +03:00
implementation 'com.github.mozilla:mozillaspeechlibrary:1.0.6'
2018-08-29 09:39:24 +03:00
}
```
2018-08-29 09:45:17 +03:00
## Demo app
Just run the demo application inside the folder `app`
2018-08-29 09:39:24 +03:00
## Usage
The API encapsulates the microphone capture, audio encoding, voice activity detection and network
communication. So for this reason its integration is very simple: just call `start()` and handle the events in your frontend.
2018-08-31 00:04:56 +03:00
#### First define a listener to capture the events:
2018-08-29 09:39:24 +03:00
```
ISpeechRecognitionListener mVoiceSearchListener = new ISpeechRecognitionListener() {
public void onSpeechStatusChanged(final MozillaSpeechService.SpeechState aState, final Object aPayload){
runOnUiThread(new Runnable() {
@Override
public void run() {
switch (aState) {
case DECODING:
// Handle when the speech object changes to decoding state
break;
case MIC_ACTIVITY:
// Captures the activity from the microphone
double db = (double)aPayload * -1;
break;
case STT_RESULT:
// When the api finished processing and returned a hypothesis
string transcription = ((STTResult)aPayload).mTranscription;
float confidence = ((STTResult)aPayload).mConfidence;
break;
case START_LISTEN:
// Handle when the api successfully opened the microphone and started listening
break;
case NO_VOICE:
// Handle when the api didn't detect any voice
break;
case CANCELED:
// Handle when a cancelation was fully executed
break;
case ERROR:
// Handle when any error occurred
string error = aPayload;
break;
default:
break;
}
}
});
}
};
```
2018-08-31 00:04:56 +03:00
#### Then start it:
2018-08-29 09:39:24 +03:00
```
mMozillaSpeechService = MozillaSpeechService.getInstance();
mMozillaSpeechService.addListener(mVoiceSearchListener);
mMozillaSpeechService.start(getApplicationContext());
```
2018-08-31 00:04:56 +03:00
#### In the case you want to cancel a progressing operation:
2018-08-29 09:39:24 +03:00
```
mMozillaSpeechService.cancel();
```
2018-08-31 00:04:56 +03:00
#### To remove a listener:
2018-08-30 23:48:36 +03:00
```
mMozillaSpeechService.removeListener(aListener);
```