I'm trying to make a use of SpeechRecognizer library in my android application, and so far its work leaves me with questions. First of all, it doesn't stop when I stop speaking.
If I try to stop speech recognizing myself, the next time it gives me 'No match!' right away.
My question is: when I use google speech recognition (f. e. when I search on web), it works like a charm. In my app it is far from perfect, though the library is the same. What is wrong with my implementation?
My code (simplified):
Note: I try to use partial results to make speech recognition more flexible, but I can't see any effect except recognition became little faster.
public void setupVoiceRecognition(Activity activity) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
mSpeechRecognizer.setRecognitionListener(this);
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
activity.getPackageName());
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,
true);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
mRecognizerIntent.putExtra(EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
mContext = activity.getApplicationContext();
if (mMainBtn != null) {
mMainBtn.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
VoiceRecognition.this.onClick();
}
});
}
}
public void forceStop() {
if (mListening) {
toggleListening(false);
}
}
public void onClick() {
toggleListening(!mListening);
}
private void toggleListening(boolean start) {
mPartialLength = 0;
if (start) {
mSpeechRecognizer.startListening(mRecognizerIntent);
} else {
mSpeechRecognizer.stopListening();
}
if (mMainBtn != null) {
mMainBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
if (mSupportBtn != null) {
mSupportBtn.setImageResource((start) ? R.drawable.icon_record_active : R.drawable.icon_record_white);
}
mListening = start;
}
...
#Override public void onError(int i) {
if (mListening) {
String errorText;
switch (i) {
case SpeechRecognizer.ERROR_AUDIO:
errorText = MyApp.getContext().getString(R.string.speech_recognition_err3);
break;
...
}
MyApp.showToast(errorText);
toggleListening(false);
if (i == NO_MATCH) {
toggleListening(true);
}
}
}
#Override public void onResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null) {
String fullText = mViewForText.getText().toString();
mViewForText.setText(fullText.substring(0, fullText.length() - mPartialLength) + matches.get(0) + " ");
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
mPartialLength = 0;
forceStop();
}
}
#Override public void onPartialResults(Bundle bundle) {
ArrayList<String> matches = bundle
.getStringArrayList(EXTRA_PARTIAL_RESULTS);
if (matches != null) {
mViewForText.setText(mViewForText.getText().toString() + matches.get(0) + " ");
mPartialLength += matches.get(0).length() + 1;
mViewForText.requestFocus(View.FOCUS_RIGHT);
mViewForText.setSelection(mViewForText.getText().length());
}
}
}
Google disabled continuous speech recognition via SpeechRecognizer for third party apps. I assume that is because they have paid API now (https://cloud.google.com/speech/) which works very well but not free.
About NO_MATCH error. Google hears own beep invitation signal and assumes it is the speech start. Such as that beep can't be recognized it returns NO_MATCH error.
There is an option. You may downgrade Google app to have more stable work of the recognition service. The last properly working version of the Google app is 6.2.34
Related
I am not sure if this is just an emulator bug or did I miss something. I am writing an alarm app. My app gets a ringtone Uri with RingToneManager, then use MediaPlayer to play the ringtone Uri. I want to control whether the ringtone loops or not. It works fine on my real smartphone, as well as with the Android Studio emulator using Uri gets with RingtoneManager.TYPE_NOTIFICATION and RingtoneManager.TYPE_RINGTONE. However, if I use Uri gets with RingtoneManager.TYPE_ALARM with the emulator, it just keeps looping even if I set mediaPlayer.setLooping(false).
This seems like an emulator bug to me. However, I want to make sure I do not miss something that I need to set. I do not want to find out after the app is released and it turned out that other people's smartphones have the same problem as the emulator. Is there anything I can do or check?
Below is the related code:
activeAlarmSound = new MediaPlayer();
activeAlarmSound.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
);
try {
activeAlarmSound.setDataSource(this, Uri.parse(ringToneString));
if (!hasBGMusic && !timeUpItem.sayDate && !timeUpItem.sayDay && !timeUpItem.sayTime) {
activeAlarmSound.setLooping(true);
} else {
activeAlarmSound.setLooping(false);
}
activeAlarmSound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (playedBGMusic && timeUpItem.getDismissMethod() == 0) {
stopSelf();
} else {
playedAlarmSound = true;
if (activeBGMusic == null) {
playBGMusic();
} else {
activeBGMusic.start();
}
if (timeUpItem.getRepeatAlarm() > 0) {
endAlarmHandler = new Handler();
endAlarmHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (activeBGMusic != null) {
activeBGMusic.pause();
}
playAlarmSoundAndTime();
}
}, timeUpItem.getRepeatAlarm() * 60000);
}
}
}
});
activeAlarmSound.prepare();
activeAlarmSound.start();
} catch (IOException e) {
e.printStackTrace();
}```
I am using WebRTC for audio call in my app. I am facing the following issue
Android App to Android App --> Call function --->Working Fine
iOS App to iOS App -->Call function --->Working Fine
Android App to iOS App -->Call function --->Not Working
In the below mentioned function, the "newState" always returning CHECKING state
#Override
public void onIceConnectionState(PeerConnection.IceConnectionState newState) {
Log.v(TAG, "ICE= " + newState);
runOnUiThread(new Runnable() {
#Override
public void run() {
if (newState == PeerConnection.IceConnectionState.CONNECTED) {
callType.setVisibility(View.GONE);
if (type.equals("audio")) {
callTime.setVisibility(View.VISIBLE);
startCountDown("answer");
} else {
updateVideoViews(true);
}
} else if (newState == PeerConnectenter code hereion.IceConnectionState.DISCONNECTED) {
callType.setVisibility(View.VISIBLE);
callType.setText(getString(R.string.poor_connection_connecting));
callTime.setVisibility(View.GONE);
} else if (newState == PeerConnection.IceConnectionState.CLOSED) {
} else {
callType.setVisibility(View.VISIBLE);
callType.setText(getString(R.string.connecting));
callTime.setVisibility(View.GONE);
}
}
});
}
The Legacy file i am using is given below link
link: https://chromium.googlesource.com/external/webrtc/+/HEAD/sdk/android/api/org/webrtc/audio/LegacyAudioDeviceModule.java
It may be caused by codec, make sure you create PeerConnectionFactory in the right way:
final PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
final DefaultVideoEncoderFactory encoderFactory = new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true);
final DefaultVideoDecoderFactory decoderFactory = new DefaultVideoDecoderFactory(eglBase.getEglBaseContext());
peerConnectionFactory = PeerConnectionFactory.builder().setOptions(options).
setVideoDecoderFactory(decoderFactory).
setVideoEncoderFactory(encoderFactory).
createPeerConnectionFactory();
I am now using the new BLE api in android developing.
Basic idea is using bluetooth scanning result to inflate the recyclerview(list);
I followed the BLE guide on google developer
Now I have two problem:
1. onBatchScanResults listener is never triggered, butonScanResult works well, is that because the scanner only sense 1 sensor nearby?
my BLE scanner is much slower compared with other applications.
The following is the two core functions' code snippet.
private void scanBLE(boolean enable) {
final BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if (enable) {
mScanning = true;
mBluetoothLeScanner.startScan(mScanCallback);
} else {
if (mScanning) {
mScanning = false;
mBluetoothLeScanner.stopScan(mScanCallback);
}
}
Log.i(TAG, "now the scanning state is" + mScanning);
}
// Device scan callback.
private ScanCallback mScanCallback =
new ScanCallback() {
public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
addBeaconTolist(result, beaconsList);
mAdapter.notifyDataSetChanged();
};
public void onScanFailed(int errorCode) {
Log.i(TAG, "error code is:" + errorCode);
};
public void onBatchScanResults(java.util.List<android.bluetooth.le.ScanResult> results) {
Log.i(TAG, "event linstener is called!!!!");
Log.i(TAG, "batch result are:" + results);
beaconsList.clear();
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
addBeaconTolist(result, beaconsList);
}
mAdapter.notifyDataSetChanged();
};
};
in MainFragment is like following:
beaconsList = new ArrayList<BeaconsInfo>();
mAdapter = new BeaconsAdapter(beaconsList);
mRecyclerView.setAdapter(mAdapter);
scannBLE(true);
Whether or not you get batch results or individual results depends on your scan settings.
In order to get batch results, you need to adjust the ScanSettings. Check out the documentation for the ScanSettings.Builder, and try using SCAN_MODE_LOW_POWER, which batches up results. You can also try adjusting the batch interval with setReportDelay(long reportDelayMillis); You can see a blog post I wrote about the power benefits of these settings here.
It's not totally clear what you mean by "my BLE scanner is much slower compared with other applications", but it may be that the app's UI lags because you are not updating it on the UI thread. Try wrapping your calls to notifyDatasetChanged like this:
runOnUiThread(new Runnable() {
#Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
Try setUseHardwareBatchingIfSupported(true). This solves the problem for me on moto360 2nd gen. I think this is auto implemented for newer API.
I followed these instructions to integrate both Libgdx and native android code using ActionResolver interface. I have no problem calling the Android method from the Libgdx part of my code. But I am hitting a dead end when I am trying to intergrate Google IAP with Libgdx. According to TrivialDrive example, it uses mPurchaseFinishedListener (outside of calling method).
My question is: how do I pass this IAP resultcode back to Libgdx since the listener is outside the calling method? Currently, purchase process went through, but the libgdx part of my code is not being "informed" of the purchase status/result.
This is my code:
Any help is much appreciated.
ActionResolver:
public interface IActionResolver {
public int requestIabPurchase(int product);
}
MainActivity:
public class MainActivity extends AndroidApplication implements IActionResolver {
// Debug tag, for logging
static final String TAG = "greatgame";
// Does the user have the premium upgrade?
boolean mIsUpgraded = false;
// SKUs for our products: the cat, all, or pow
static final String SKU_UPGRADE = "android.test.purchased";
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10001;
// The helper object
IabHelper mHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new Catland(this), cfg);
}
void iAbStartup() {
String base64EncodedPublicKey = "some key";
// Create the helper, passing it our context and the public key to verify signatures with
Log.d(TAG, "Creating IAB helper.");
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up in-app billing: " + result);
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
}
// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) {
return;
}
// Is it a failure?
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
// Do we have the SKU_UPGRADE upgrade?
Purchase thisUpgrade = inventory.getPurchase(SKU_UPGRADE);
mIsUpgraded = (thisUpgrade != null && verifyDeveloperPayload(thisUpgrade));
Log.d(TAG, "User is " + (mIsUpgraded ? "Upgraded" : "Free"));
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
runPurchaseFlow(submitProduct);
}
};
// Run real purchase flow
public void runPurchaseFlow(int product) {
Log.d(TAG, "runPurchaseFlow");
/* TODO: for security, generate your payload here for verification. See the comments on
* verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
* an empty string, but on a production app you should carefully generate this. */
String payload = "";
if (product == 1)
mHelper.launchPurchaseFlow(this, SKU_UPGRADE, RC_REQUEST, mPurchaseFinishedListener, payload);
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null) return;
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
if (!verifyDeveloperPayload(purchase)) {
Log.d(TAG, "Error purchasing. Authenticity verification failed.");
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_CAT)) {
// bought the upgrade!
Log.d(TAG, "Purchase Upgrade. Congratulating user.");
mIsUpgraded = true;
// how do i pass this result to the libgdx?
}
}
};
/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
return true;
}
#Override
public int requestIabPurchase(int product) {
iAbStartup();
return 0; // how do i get the result from mPurchaseFinishedListener?
}
}
PurchaseScreen
result = greatgame.actionResolver.requestIabPurchase(1);
You won't be able to return the result from requestIabPurchase() - the only methods of doing so would block for a long time. The best way, in my opinion, would be to create a listener interface of your own that your LibGdx project implements, and pass that into your request interface. For example:
In your libGdx project somewhere:
interface PurchaseCallback {
public int setPurchaseResult(int result);
}
ActionResolver:
public interface IActionResolver {
public int requestIabPurchase(int product, PurchaseCallback callback);
}
In PurchaseScreen, implement PurchaseCallback:
#override
public int setPurchaseResult(int result) {
// Yay! I have a result from a purchase! Maybe you want a boolean instead of an int? I don't know. Maybe an int (for the product code) and a boolean.
}
...and pass whatever is implementing PurchaseCallback (I'm assuming your PurchaseScreen does itself):
result = greatgame.actionResolver.requestIabPurchase(1, this);
Finally, hook it all up in MainActivity:
PurchaseCallback mCallback = null;
mPurchaseFinishedListener = ... etc. etc.
.
.
.
if (mCallback != null) {
mCallback.setPurchaseResult(0);
}
.
.
.
#Override
public int requestIabPurchase(int product, PurchaseCallback callback) {
mCallback = callback; // save this for later
iAbStartup();
return 0;
}
Note that you should call PurchaseCallback.setPurchaseResult() everywhere that mPurchaseFinishedListener has return, not only at the line // how do i pass this result to the libgdx? - otherwise, you will never know if a purchase failed or is just taking a really long time.
I have a CC2540 BLE kit and i want to see the battery level of the kit in my smartphone app. I have made the code, which gets the battery level from the kit. The battery level is called BodySensor in this case:
First, i will go into the characteristic value of the battery service part for the kit. So i use the Service and Characteristic UUID's for the battery service:
public void getBodySensorLoc(BluetoothDevice device)
{
Log.i(TAG, "getBodySensorLoc");
BluetoothGattService mHRP = mBluetoothGatt.getService(device, HRP_SERVICE);
if (mHRP == null)
{
Log.e(TAG, "getBodySensorLoc: mHRP = null");
return;
}
BluetoothGattCharacteristic mBSLcharac = mHRP.getCharacteristic(BODY_SENSOR_LOCATION);
if (mBSLcharac == null) {
Log.e(TAG, "getBodySensorLoc: mBSLcharac = null");
return;
}
mBluetoothGatt.readCharacteristic(mBSLcharac);
}
My purpose is to read the battery level, when pressing a button in my application. So i implement this button in my Activity class:
((Button) findViewById(R.id.btn_BSL)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
mService.getBodySensorLoc(mDevice);
}
});
This kit will automatically send the battery level back to the smartphone. So i will set focus on onCharacteristicRead method of BluetoothGattCallback part in my code:
private BluetoothGattCallback mGattCallbacks = new BluetoothGattCallback()
{
public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status)
{
UUID charUuid = charac.getUuid();
Bundle mBundle = new Bundle();
Message msg = Message.obtain(mActivityHandler, HRP_VALUE_MSG);
Log.i(TAG, "onCharacteristicRead");
if (charUuid.equals(BODY_SENSOR_LOCATION))
mBundle.putByteArray(BSL_VALUE, charac.getValue());
msg.setData(mBundle);
msg.sendToTarget();
}
};
I want the application to show the received battery level in a textview. The kit sends the battery level as a integer, so if the level is 70%, it sends "70". The code includes a Handler in the Activity class:
private Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HRPService.HRP_VALUE_MSG:
Log.d(TAG, "mHandler.HRP_VALUE_MSG");
Bundle data1 = msg.getData();
final byte[] bslval = data1.getByteArray(HRPService.BSL_VALUE);
runOnUiThread(new Runnable()
{
public void run()
{
if (bslval != null)
{
try {
Log.i(TAG, "BYTE BSL VAL =" + bslval[0]);
TextView bsltv = (TextView) findViewById(R.id.BodySensorLocation);
bsltv.setText("\t" + mContext.getString(R.string.BodySensorLocation)
+ getBodySensorLocation(bslval[0]));
} catch (Exception e)
{
Log.e(TAG, e.toString());
}
}
}
});
default:
super.handleMessage(msg);
}
}
};
The problem is that the textview doesn't show anything, when i press this button in my application after i connected my smartphone with the kit. Can anyone tell me, where the problem is ?.
Thanks in advance
Unsure what exact kit you have. Unsure if they all have battery level measuring hardware on board. Maybe this is the reason.