Get transcription of recognized data using SpeechRecognizer - java

I use SpeechRecognizer for implement a "speech-to-text" functional and result of her work is a text data:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString());
intent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{});
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
recognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {}
#Override
public void onBeginningOfSpeech() {}
#Override
public void onRmsChanged(float rmsdB) {}
#Override
public void onBufferReceived(byte[] buffer) {}
#Override
public void onEndOfSpeech() {}
#Override
public void onError(int error) {}
#Override
public void onPartialResults(Bundle partialResults) {}
#Override
public void onEvent(int eventType, Bundle params) {}
#Override
public void onResults(Bundle results) {
String result = null;
ArrayList<String> arrOfResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String command = arrOfResults.get(0);
}
});
recognizer.startListening(intent);
Is it possible to get a transcription of recognized speech instead of text using this method?
For example, as it was implemented in Google Translate:
Or, if necessary, use a different approach.
How can i do this? Thanks in advance. Regards...

The SpeechReocgnizer API only offers the "text" (normal orthographic text, I guess), and optionally the confidence scores, see: https://developer.android.com/reference/android/speech/RecognitionListener.html#onResults(android.os.Bundle)

Related

Getting error while setting speechRecognition.startListenenig() and pass the intent

I get error when I set SPR.startlistenening() method I want to use continuously speech recognition and perform tasks based on results.
I am making an app that countinuously uses speech recognition and do specific task on results:
#Override
protected void onStart() {
super.onStart();
setSPR();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
SPR.startListening(intent);
}
private void setSPR() {
if (SpeechRecognizer.isRecognitionAvailable(this)){
SpeechRecognizer.createSpeechRecognizer(this);
SPR.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int error) {
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
setRecognitionResults(results.get(0));
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) { }
});
}
}
Error >>>
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.speech.SpeechRecognizer.startListening(android.content.Intent)'
on a null object reference at
com.teamdev.talkingtorch.MainActivity.onStart(MainActivity.java:74) at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1341)
at android.app.Activity.performStart(Activity.java:7278) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)

Trigger Speech Recognition in Android Service

I have been trying to find a way to implement the SpeechRecognizer API in a Service (runs in background) so that when a condition is met, it will open the speech recognizer without having to be within the application. My question is whether this is even possible natively? And if so, how would it be done?
Here is my code snippet. You can use the recognition listener like this in a service.
I'm not sure how you are scheduling your services, I have left that to you. But you can do something like this. (I have not added code for restarting service / starting it in a timer etc.)
public class MyService extends Service {
protected static SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
Context c;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//if condition is met then do this
SpeechRecognitionListener h = new SpeechRecognitionListener();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(h);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
Log.d("avail", " " + mSpeechRecognizer.isRecognitionAvailable(this));
if (mSpeechRecognizer.isRecognitionAvailable(this))
Log.d("created", "onBeginingOfSpeech");
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
return START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
c= getApplicationContext();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
class SpeechRecognitionListener implements RecognitionListener {
#Override
public void onReadyForSpeech(Bundle bundle) {
Log.d("onReady", "service");
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float v) {
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int i) {
Log.d("ERROR","ERROR");
}
#Override
public void onResults(Bundle resultsBundle) {
Log.d("Results", "onResults");
}
#Override
public void onPartialResults(Bundle bundle) {
}
#Override
public void onEvent(int i, Bundle bundle) {
}
}
}

Loading multiple AdMob videos

For demonstration purposes, the app has one activity that simply offers this:
You click a button, view a rewarded video, and you are rewarded with whatever.
The Problem
How can I load the videos? From what I have seen you can only call mAd.loadAd() once. There are 3 videos, each with their own AD UNIT ID. Each ad unit can have its own listener, but only one video loads so it doesn't matter...
When trying to load multiple videos
For example:
mAd1.loadAd("AD_UNIT_1", new AdRequest.Builder().build());
mAd2.loadAd("AD_UNIT_2", new AdRequest.Builder().build());
mAd3.loadAd("AD_UNIT_3", new AdRequest.Builder().build());
results in only the last video being loaded and this in log:
W/Ads: Loading already in progress, saving this object for future refreshes.
onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAd1 = MobileAds.getRewardedVideoAdInstance(this);
mAd2 = MobileAds.getRewardedVideoAdInstance(this);
mAd3 = MobileAds.getRewardedVideoAdInstance(this);
listeners...
mAd1.loadAd() etc
}
Thank you for your help
Edit: It's clear I am thinking about this problem wrong. I have 5+ ad zones that each will play a rewarded video and give a different reward (for example, one gives coins, one gives a level up, and so on..). There is no reason to load 5 videos. I should load one in onCreate(), so it's ready when needed, then load it again after the item is rewarded so it's ready for next time.
So the question remains, if there is just the one video, and thus one ad zone, being loaded onCreate() then how can I track what reward to give?
Here's a simple solution...
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
#Override
public void onRewarded(RewardItem rewardItem) {
switch(Constants.currentAd) {
case("REWARD1"):
//do something
Constants.currentAd = "";
break;
case("REWARD2"):
//do something
Constants.currentAd = "";
break;
case("REWARD3"):
//do something
Constants.currentAd = "";
break;
}
}
});
mAd.loadAd("REWARDED_VIDEO_UNIT_ID", new AdRequest.Builder().build());
}
public void showRewardedVideo() {
if (mAd.isLoaded()) {
mAd.show();
}
}
Constants.java
public class Constants {
public static String currentAd = "";
}
Showing the ad after button click
rewardButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Constants.currentAd = "REWARD1";
dismiss();
((MainActivity) getActivity()).showRewardedVideo();
}
});
REWARDED_VIDEO_UNIT_ID is one ad unit for rewarded video in AdMob...remove the rest. No need for other units, you can track whatever you like in the listener.
Other simple soluction...
AbstractRewardVideo.java
public abstract class AbstractRewardVideo {
private RewardedVideoAd mAd;
private String adId = "ca-app-pub...";
private Activity activity;
abstract protected RewardedVideoAdListener getListener();
public void init(Activity activity) {
this.activity = activity;
mAd = MobileAds.getRewardedVideoAdInstance(activity);
setAdId(adId);
loadRewardedVideoAd();
}
public Activity getActivity(){
return this.activity;
}
public void loadRewardedVideoAd() {
mAd.loadAd(adId, new AdRequest.Builder().build());
}
public void showVideo(){
setListener(getListener());
if (mAd.isLoaded()) {
mAd.show();
} else {
Utils.exibirToast("Don't loaded!");
}
}
public void setAdId(#NonNull String id){
this.adId = id;
}
public void setListener(RewardedVideoAdListener listener){
mAd.setRewardedVideoAdListener(listener);
}
}
Reward1.java
public class Reward1 extends AbstractRewardVideo {
public Reward1(Activity activity) {
init(activity);
}
#Override
protected RewardedVideoAdListener getListener() {
return new Listener();
}
private class Listener implements RewardedVideoAdListener {
#Override
public void onRewarded(RewardItem rewardItem) {
//Do something...
}
public void onRewardedVideoAdLoaded() {}
public void onRewardedVideoAdOpened() {}
public void onRewardedVideoStarted() {}
public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
public void onRewardedVideoAdLeftApplication() {}
public void onRewardedVideoAdFailedToLoad(int i) {}
}
}
Reward2.java
public class Reward2 extends AbstractRewardVideo {
public Reward2(Activity activity) {
init(activity);
}
#Override
protected RewardedVideoAdListener getListener() {
return new Listener();
}
private class Listener implements RewardedVideoAdListener {
#Override
public void onRewarded(RewardItem rewardItem) {
//Do something...
}
public void onRewardedVideoAdLoaded() {}
public void onRewardedVideoAdOpened() {}
public void onRewardedVideoStarted() {}
public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
public void onRewardedVideoAdLeftApplication() {}
public void onRewardedVideoAdFailedToLoad(int i) {}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity{
Reward1 reward1;
Reward2 reward2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reward1 = new Reward1(this);
reward2 = new Reward1(this);
...
reward1.showVideo();
...
reward2.showVideo();
}
}
MobileAds.initialize ( this, "ca-app-pub-4761500786576152~8215465788" );
RewardedVideoAd mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(Video_Ad.this);
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
#Override
public void onRewardedVideoCompleted() {
}

Callback weird behaviour (Android, Picasso library)

I am using Picasso library to manage my image uploading and caching. When I am trying to execute this code:
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropImage(photo); //not getting here
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
});
I am not entering int the onBitmapLoaded callback. Only if I close the activity (going back) and re opening it I see the image (entering into onBitmapLoaded).
But if I will change my code by just adding some Toast message into the onPrepareLoad callback every thing works fine. here is the full code:
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
Toast.makeText(thisActivity, "message", Toast.LENGTH_LONG).show();
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropImage(photo);
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
});
Why the Toast makes it work? what is wrong with it?
I solved the issue by declaring a Target instance as class member. then initialised it. Like this:
target = new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropEventImage(photo);
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
};
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(target);

Gplus user authentication and Getting user location in the same activity

In my activity i am implementing the below given classes
com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks,
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
These two interfaces is for authenticating user through google plus.
com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
and these are for getting user current location
com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks,
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener
the methods have in these classes are same.
#Override
public void onConnected(Bundle connectionHint) {}
and
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {}
Since these methods have the same parameters and same return type i cant have two in the same class. So i think i need to identify which interface have invoked from the Bundle or ConnectionResult . How i can do this ? I mean which key value i need to check ? If need any clarification please comment.
Thank you
What about implementing the interfaces as a anonymous member declaration ?
public class Ac {
private GooglePlayServicesClient.OnConnectionFailedListener psConnectionFailedListener =
new GooglePlayServicesClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// implementation
}
};
private GooglePlayServicesClient.ConnectionCallbacks psConnectionCallbacks =
new GooglePlayServicesClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle bundle) {
// implementation
}
#Override
public void onDisconnected() {
// implementation
}
};
private GoogleApiClient.ConnectionCallbacks googleConnectionCallbacks =
new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle bundle) {
// implementation
}
#Override
public void onConnectionSuspended(int i) {
// implementation
}
};
private GoogleApiClient.OnConnectionFailedListener googleConnectionFailedListener =
new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// implementation
}
};
}

Categories

Resources