onUtteranceCompleted() lost if TTS received is too long - android

I just observed an "undocumented anomaly" in Android's TTS engine: If the text to be spoken is too long (greater than 4K characters), then onUtteranceCompleted() for that particular utterance will never arrive...
Has anyone else come across this?
Is this a known bug or limitation?
What could be done to work around this?

I wasn't aware of the limit, as I prefer smaller chunks of speech (useful if pausing or if activity is paused).
When you call speak, you can add the new utterance to the end of the queue using this for queueMode:
TextToSpeech.QUEUE_ADD
Test to be sure the sentence doesn't sound different, but I think just automatically parsing at the next sentence (or word if needed) after a cutoff length would work.

I am not sure if this will be helpful in your case, but in a similar situation I used an anonymous broadcast reciever with an IntentFilter for TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED as given below
filter = new IntentFilter(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
receiver = new BroadcastReceiver(){
public void onReceive(Context p1, Intent p2)
{
if (p2.getAction().equals(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED) && tts != null)
{
//
//code here
}
}
};
context.registerReceiver(receiver, filter);
tts = new TextToSpeech(context, this);
Hope this could be of some help for someone at sometime

Related

Turning notifications to text-to-speech when driving

I have an application that according to some events, changes a normal notification to text-to-speech in order to since sometimes the phone isn't available to users, and it'll be safer not to handle the phone.
For example, when you're driving, this is dangerous, so i want to turn the notifications to text-to-speech.
I've looked for a long time some explanation for turning text-to-speech when driving, but i can't find any reference for that no where i search.
For generating text-to-speech, i have this part, which works fine :
private TextToSpeech mTextToSpeech;
public void sayText(Context context, final String message) {
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
try {
if (mTextToSpeech != null && status == TextToSpeech.SUCCESS) {
mTextToSpeech.setLanguage(Locale.US);
mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, null);
}
} catch (Exception ex) {
System.out.print("Error handling TextToSpeech GCM notification " + ex.getMessage());
}
}
});
}
But, i don't know how to check if i'm currently driving or not.
As Ashwin suggested, you can use Activity recognition Api, but there's a downside of that, the driving samples you'll receive, has a field of 'confidence' which isn't always accurate, so you'll have to do extra work(such as check locations to see if you actually moved) in order to fully know if the user moved.
You can use google's FenceApi which allows you to define a fence of actions such as driving, walking, running, etc. This api launched recently. If you want a sample for using it, you can use this answer.
You can pull this git project (everything free), which does exactly what you want : adds to the normal notification a text-to-speech when you're driving.
In order to know whether you are driving or not you can use Activity Recognition API
Here is a great tutorial that might help you out Tutorial and Source Code

clear missed calls and clear notification from android bar

Using this code I managed to mark all missed calls as read:
ContentValues values = new ContentValues();
values.put(Calls.NEW, 0);
if (android.os.Build.VERSION.SDK_INT >= 14) {
values.put(Calls.IS_READ, 1);
}
StringBuilder where = new StringBuilder();
where.append(Calls.NEW);
where.append(" = 1 AND ");
where.append(Calls.TYPE);
where.append(" = ?");
context.getContentResolver().update(Calls.CONTENT_URI, values, where.toString(),
new String[]{ Integer.toString(Calls.MISSED_TYPE) });
but in the android notification bar I still have a flag with missed calls. How can I also clear the notification bar for calls in android?
How can I also clear the notification bar for calls in android?
You don't. That Notification is put there by another app, and you have no means of controlling whether that Notification is displayed, short of building a ROM mod that changes the behavior of that other app.
UPDATE: Since this answer was originally written, NotificationListenerService was added and can clear notifications, but only on Android 4.3+.
The only "legal" but extremely ugly and usually useless way to achieve what you want is to show Call Log to user. And I mean literally show (becomes visual, gets focus). In case you want to do this, here's how:
public static boolean showCallLog(Context context)
{
try
{
Intent showCallLog = new Intent();
showCallLog.setAction(Intent.ACTION_VIEW);
showCallLog.setType(android.provider.CallLog.Calls.CONTENT_TYPE);
context.startActivity(showCallLog);
return true;
}
catch (Exception e)
{
Log.d("Couldn't show call log.", e.getMessage());
}
return false;
}
The reason behind this mess is the fact that apps authoritatively responsible for call logging and notifying users about missed calls (stock phone apps) use cached values. Why? Because of overall performance. You need to somehow notify those apps that Call Log has changed (seen means changed, as well) and that it should update it. It would be nice if all such apps on all devices would receive a broadcast in order to refresh, but as far as I know, it's not the case.
I hope someone will find a better way (without interrupting the user) to force refresh on stock phone apps.

CWAC LocationPoller - Broadcast not being received

I was linked to your library (once again, THANKS and excellent work).
I've been trying for the past two hours what the problem is but I've failed.
Here's the AlarmManager in my Login screen code:
Intent i = new Intent(con, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(con,
LocationReceiver.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER,
LocationManager.NETWORK_PROVIDER);
gps = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(con, 0, i, PendingIntent.FLAG_NO_CREATE);
gps.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
10 * 1000, pi);
Log.d("Service: ",
"GPS Service started and scheduled with AlarmManager");
Here is the class I created myself (not the one from your demo, though similar):
public class LocationReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle b = intent.getExtras();
Location loc = (Location) b.get(LocationPoller.EXTRA_LOCATION);
String msg;
if (loc == null)
{
loc = (Location) b.get(LocationPoller.EXTRA_LASTKNOWN);
if (loc == null)
{
msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
}
else
{
msg = "TIMEOUT, lastKnown=" + loc.toString();
}
}
else
{
msg = loc.toString();
}
if (msg == null)
{
msg = "Invalid broadcast received!";
}
Log.d("GPS Broadcast: ", "Location: " + msg);
}
}
Nothing's happening. I assume this because I'm not seeing any info in my logcat, at all. Plus the PollerThread (if that's the right term) keeps piling up when I look at the debug view, as if they're all waiting for something but not sending any broadcasts.
What am I doing wrong? Ascertaining location via Network shouldn't take much time should it? Even if that was the issue, I should have gotten SOME feedback..
Here are the entries in my manifest's application tag:
<receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />
<receiver android:name=".LocationReceiver" />
<service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />
LocationPoller is designed for much longer polling periods: an hour, not 10 seconds. I have never tested LocationPoller with that frequent of a polling period, nor do I ever intend to support LocationPoller with a polling period shorter than the DEFAULT_TIMEOUT of two minutes.
Hence, the first thing you should do is figure out why you are bothering with LocationPoller in the first place. The point behind LocationPoller is to be able to occasionally figure out where the device is, without your code necessarily already running (e.g., you want to automatically check into Foursquare every hour). If you need to find out where the device is every 10 seconds, you need to use LocationManager yourself from a foreground activity.
Assuming you are convinced that LocationPoller is still the right solution, the next thing you should do is extend your polling period to something more reasonable, like 5 minutes, and see if that helps.
If that does not help, then try running the demo/ project without modification. If that does not work, then there's some bug in LocationPoller, or some incompatibility in your device that LocationPoller is tripping over, and I'd need to do some investigation.
If the demo/ project does work, make a copy, confirm the copy works, and then slowly modify the copy to look more like what your production code has, until it stops working -- at that point, your last set of changes represent what broke LocationPoller, and we can figure out if that represents a bug in LocationPoller or a bug in your use of it.

Unregistering SensorManager doesn't work

In my app, I am using the light and proximity sensor to detect phone out of pocket functionality and then unregistering the SensorManager when their detection is complete. But even when the CPU usage by the app shows just 1-2 sec usage, the battery usage always shows my app as no. 1 app in the list which is worrying.
I have used the SensorManager.unRegisterListener and also set SensorManager = null, but the situation remains the same.
I have read, that due to some bug, the sensors are not unregistered correctly. Any good way to dispose the sensors correctly ?
Pls guide.
Omkar Ghaisas
Updated with Code sample from app -
#Override
protected void onPause()
{
super.onPause();
unHookReceiver();
}
private void unHookReceiver()
{
if (r != null)
{
unregisterReceiver(r);
if(GetProximityPreference("EnableReceiveByProximity"))
{
mySensorManager.unregisterListener(proximitySensorEventListener);
mySensorManager.unregisterListener(lightSensorEventListener);
mySensorManager = null;
FileUtils.appendLog(FileUtils.GetCurrentDateTime() + " Power Consumption Log End");
FileUtils.appendLog("------------------------------------------------");
}
r = null;
}
}
I am also setting the sensorManager = null as per one suggestion from one post on stackpverflow, but even that doesn't help. In spite of calling the cleanup code, the battery usage is still very high. The app by itself should not be using much battery as its a very simple app with just one broadcast receiver and one activity, but within the activity, I invoke the Light and Proximity sensors and I doubt those are causing the spike in battery usage. Not sure why though.
Any help is highly appreciable.
mSensorManager.registerListener(YourListener.this, mSensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
SensorManager.SENSOR_DELAY_NORMAL);
take this to register your Listener... then works your unregisterListener
Just put this register code into main handler, then it works. but I don't know why.
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
#Override
public void run() {
if (mSensorManager != null) {
mSensorManager.registerListener(sensorEventListener, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
}
});
I was able to resolve this by correctly matching when the listeners were registered and when they were unregistered. Perhaps, initially the listeners weren't getting properly unregistered in all Call conditions (incoming call, outgoing call, missed call etc), so even when the activity closed, the listeners were still listening for events thus unnecessarily consuming power.

fade in and out music while speaking a text

I have an Android application that makes use of TTS (Text to speech) API. Everything is working perfectly, but now i want to fade in/out or even stop music (in case user is playing music with prebuilt Music Player), when application speaks a text. Right now, i think both music and TTS messages are played on the same stream (MUSIC), and it can be difficult to understand the voice messages.
I've tried to play the text on a different stream, like AudioManager.STREAM_NOTIFICATIONS. It does stop the music, but doesn't come back when the text is spoken, so i didn't achieve the goal. Haven't found anything yet, so i hope someone can help here. Thanks!
I finally got something that is working. Not perfect though. A quite dirty trick. Just in case it can help to someone:
This is fixed on API 8 with requestAudioFocus and abandomAudioFocus methods of AudioManager.
But for former versions, you can try this. Play TTS through a different stream channel, let's say STREAM_NOTIFICATIONS. Then you just need to return audio focus to STREAM_MUSIC. How can you achieve that?. Sending a silence string (" ") to TTS but this time through STREAM_MUSIC. The effect will be: music is stopped, your TTS message gets spoken, and finally your music is back after the voice alert. Not nice or something to feel proud of, but... if someone knows of a different way, i will appreciate it
Here is a way of doing this in Dec-2021
TexToSpeech needs to be initialized and assigned to tts before calling this method
Method 1 (Recommended):
private void speak(String textToSay) {
AudioAttributes mPlaybackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANT)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
//add this below flag if you need the TTS to speak in a louder volume or TTS volume be heard for sure at any cost
//.setFlags(FLAG_AUDIBILITY_ENFORCED)
.build();
tts.setAudioAttributes(mPlaybackAttributes);
AudioFocusRequest mFocusRequest =
new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
.setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(false)
.setWillPauseWhenDucked(false)
.build();
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(mFocusRequest);
tts.speak(textToSay, TextToSpeech.QUEUE_FLUSH, null, textToSay);
Handler ttsSpeak = new Handler();
Runnable checkTTSRunning = new Runnable() {
#Override
public void run() {
if (tts.isSpeaking()) {
ttsSpeak.postDelayed(this, 1000);
} else am.abandonAudioFocusRequest(mFocusRequest);
}
};
ttsSpeak.postDelayed(checkTTSRunning, 3000);
}
Method 2: Use this only if you need the TTS to speak in a louder volume and/or TTS volume needs to be heard for sure at any cost
private void speak(String textToSay) {
AudioAttributes mPlaybackAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANT)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setFlags(FLAG_AUDIBILITY_ENFORCED) //VERY IMPORTANT
.build();
tts.setAudioAttributes(mPlaybackAttributes);
tts.speak(textToSay, TextToSpeech.QUEUE_FLUSH, null, textToSay);
}
Could you use the TextToSpeech.OnUtteranceCompletedListener along with AudioManager.setStreamVolume to achieve this?

Categories

Resources