I m working on the Translator application where I need to speak out what user has translated. By following the Huawei Text to Speech Doc I m getting the Error.
onError: MLTtsError{errorId=11301, errorMsg='The speaker is not supported. ', extension=7002}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak_and_translate);
showInterstitialAd();
MLApplication.getInstance().setApiKey("Your Key");
if (deviceManufacture.equalsIgnoreCase("Huawei")) {
setUpHuaweiTts();
}
}
private void setUpHuaweiTts() {
mlTtsConfig = new MLTtsConfig()
// Set the text converted from speech to English.
// MLTtsConstants.TtsEnUs: converts text to English.
// MLTtsConstants.TtsZhHans: converts text to Chinese.
.setLanguage(MLTtsConstants.TTS_EN_US)
// Set the English timbre.
// MLTtsConstants.TtsSpeakerFemaleEn: Chinese female voice.
// MLTtsConstants.TtsSpeakerMaleZh: Chinese male voice.
.setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)
// Set the speech speed. Range: 0.2–1.8. 1.0 indicates 1x speed.
.setSpeed(1.0f)
// Set the volume. Range: 0.2–1.8. 1.0 indicates 1x volume.
.setVolume(1.0f);
mlTtsEngine = new MLTtsEngine(mlTtsConfig);
mlTtsEngine.setTtsCallback(new MLTtsCallback() {
#Override
public void onError(String s, MLTtsError mlTtsError) {
Log.d(TAG, "onError: "+ mlTtsError);
}
#Override
public void onWarn(String s, MLTtsWarn mlTtsWarn) {
Log.d(TAG, "onWarn: ");
}
#Override
public void onRangeStart(String s, int i, int i1) {
Log.d(TAG, "onRangeStart: ");
}
#Override
public void onAudioAvailable(String s, MLTtsAudioFragment mlTtsAudioFragment, int i, Pair<Integer, Integer> pair, Bundle bundle) {
Log.d(TAG, "onAudioAvailable: ");
}
#Override
public void onEvent(String s, int i, Bundle bundle) {
// Callback method of a TTS event. eventId indicates the event name.
switch (i) {
case MLTtsConstants.EVENT_PLAY_START:
Log.d(TAG, "onEvent: Play");
// Called when playback starts.
break;
case MLTtsConstants.EVENT_PLAY_STOP:
// Called when playback stops.
boolean isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED);
Log.d(TAG, "onEvent: Stop");
break;
case MLTtsConstants.EVENT_PLAY_RESUME:
// Called when playback resumes.
Log.d(TAG, "onEvent: Resume");
break;
case MLTtsConstants.EVENT_PLAY_PAUSE:
// Called when playback pauses.
Log.d(TAG, "onEvent: Pause");
break;
// Pay attention to the following callback events when you focus on only synthesized audio data but do not use the internal player for playback:
case MLTtsConstants.EVENT_SYNTHESIS_START:
// Called when TTS starts.
Log.d(TAG, "onEvent: SynStart");
break;
case MLTtsConstants.EVENT_SYNTHESIS_END:
// Called when TTS ends.
Log.d(TAG, "onEvent: SynEnd");
break;
case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
// TTS is complete. All synthesized audio streams are passed to the app.
boolean isInterruptedCheck = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED);
Log.d(TAG, "onEvent: SynComplete");
break;
default:
break;
}
}
});
mlTtsEngine.speak("test", MLTtsEngine.QUEUE_APPEND);
}
Currently, I m just setting string "test" for test purpose. I have to get the text from Model and set it for the speaking. I couldn't see anything like that in Doc regarding speaker Error.I have searched For the ErrorCode on Huawei.
public static final int ERR_ILLEGAL_PARAMETER
Invalid parameter.
Constant value: 11301
LogCat: Debug
LogCat: Error
Any help would be really appreciated. Thanks.
I have set the wrong person with English language. So changing this code of Line
.setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)
with
.setPerson(MLTtsConstants.TTS_SPEAKER_MALE_EN)
works perfectly fine.
Related
Unable to override Here SDK to disable sound effect on the onSpeedExceeded event.
Using the Here Developer tutorial, (https://developer.here.com/blog/android-premium-sdk-speed-limit-warning-example), I succeeded in running the sample app. But...
While driving, when I exceed the speed limit, there is a doot doot doot. I want to override this behaviour as I intend to use my own sounds.
I guessed that I might override the code by creating a NavigationManager.SpeedWarningListener. Unfortunately I can not disable or defeat the 'onSpeedExceeded' sound effects.
NavigationManager.SpeedWarningListener speedWarningListener = new NavigationManager.SpeedWarningListener() {
#Override
public void onSpeedExceeded(String s, float v) {
//super.onSpeedExceeded(s, v);
//Log.v(Global.TAG, "onSpeedExceeded");
Global.SpeedLimitExceeded = true;
}
#Override
public void onSpeedExceededEnd(String s, float v) {
//super.onSpeedExceededEnd(s, v);
//Log.v(Global.TAG, "onSpeedExceededEnd");
Global.SpeedLimitExceeded = false;
}
};
EDITED ANSWER: This method needs to be amended to stop the speed warning:
private void startNavigationManager() {
NavigationManager.Error navError = NavigationManager.getInstance().startTracking();
// added by suggestion from stackoverflow
NavigationManager.getInstance().stopSpeedWarning();
if (navError != NavigationManager.Error.NONE) {
Log.d(Global.TAG, "NavigationManager: false");
//handle error navError.toString());
} else {
//Log.d(Global.TAG, "NavigationManager: true");
}
}
Please set speedWarningEnabled accordingly for NMANavigationManager
navigationManager:didUpdateSpeedingStatus:forCurrentSpeed:speedLimit: will be sent to the delegate when speeding is detected or when a correction is made.
Also refer http://developer.here.com/documentation/ios-premium/api_reference_jazzy/Classes/NMANavigationManager.html
I'd like to ask you for some help with Android TextToSpeech feature.
Basically, I'd like to develop a simple AI which speaks, asking a question then waits for an answer, and at last, based on answer asks another question and so on, until user pronounces a keyword which stops everything.
Now I know TextToSpeech has to be initialized before using speak method, and I'm trying to take this into account by using onActivityResult method.
Below some code:
Activity class:
public class MainActivity extends AppCompatActivity implements OnInitListener, Button.OnClickListener{
Button sayHello;
TextView textView;
private static final int CHECK_DATA = 0;
private static final Locale defaultLocale = Locale.UK; // British English
private static final String TAG = "TTS";
private TextToSpeech tts;
private boolean isInit = false;
sayIt Method: used to speak:
public void sayIt(String text, boolean flushQ){
if(isInit){
if(flushQ){
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_ADD, null, null);
}
} else {
Log.i(TAG, "Failure: TTS instance not properly initialized");
}
}
TextToSpeech Listener:
#Override
public void onInit(int status){
if(status == TextToSpeech.SUCCESS){
isInit = true;
// Enable input text field and speak button now that we are initialized
sayHello.setEnabled(true);
// Set to a language locale after checking availability
Log.i(TAG, "available="+tts.isLanguageAvailable(Locale.UK));
tts.setLanguage(defaultLocale);
// Examples of voice controls. Set to defaults of 1.0.
tts.setPitch(1.0F);
tts.setSpeechRate(1.0F);
// Issue a greeting and instructions in the default language
tts.speak("Initialized!", TextToSpeech.QUEUE_FLUSH, null, Integer.toString(12));
} else {
isInit = false;
Log.i(TAG, "Failure: TTS instance not properly initialized");
}
}
Button Listener:
#Override
public void onClick(View v){
if(isInit)
sayIt("You clicked!", true);
}
onActivityResult Method:
// Create the TTS instance if TextToSpeech language data are installed on device. If not
// installed, attempt to install it on the device.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CHECK_DATA) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// Success, so create the TTS instance. But can't use it to speak until
// the onInit(status) callback defined below runs, indicating initialization.
Log.i(TAG, "Success, let's talk");
tts = new TextToSpeech(this, this);
// Use static Locales method to list available locales on device
Locale[] locales = Locale.getAvailableLocales();
Log.i(TAG,"Locales Available on Device:");
for(int i=0; i<locales.length; i++){
String temp = "Locale "+i+": "+locales[i]+" Language="
+locales[i].getDisplayLanguage();
if(locales[i].getDisplayCountry() != "") {
temp += " Country="+locales[i].getDisplayCountry();
}
Log.i(TAG, temp);
}
} else {
// missing data, so install it on the device
Log.i(TAG, "Missing Data; Install it");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
And, at last, onCreate Method:
#Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
sayHello = findViewById(R.id.sayBtn);
textView = findViewById(R.id.textView);
sayHello.setEnabled(false);
sayHello.setOnClickListener(this);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, CHECK_DATA);
/* THIS SPEAK DOES NOT WORK! */
sayIt("Speech from method!", true);
}
Issue is: Button successfully gets enabled when onInit method initialises TextToSpeech and successfully pronounces text.
My goal is to make the Activity speak from onCreate method, since at the moment it only works from onInit and onClick listeners, bot not in onCreate, even if I check for tts initialization using onActivityResult.
Basically I want the TextToSpeech to speak with no Buttons involved.
I know very similar questions were already posted, but none solved my problem. Have some idea?
Hope I've been clear, Thank you!
UPDATE: Log shows ERROR detected occurs in else branch of onInit method, where Log.i(TAG, "Failure: TTS instance not properly initialized"); line is.
SOLUTION:
The only thing to do here is to wait a little time in order to let TextToSpeech initialize for good.
A good way seems to be by using a delayed Handler as follows:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Waiting for RobotTextToSpeech initialization for 1500ms
rtts.speak("This speak will work!");
rtts.speak("This other speak will work too!");
}
}, 1500);
}
By doing this, looks like TextToSpeech works well even in onCreate method, we just have to wait little time.
Hope this can help.
I have built an app which lets the user control their scrolling action using the fingerprint sensor.
It used to work earlier until some weeks back, where I found that method: isGestureDetectionAvailable() always returns 'False' after starting 'accessibility service'
Since I am getting 'isGestureDetectionAvailable()' as always 'False',
my 'registerFingerprintGestureCallback' doesn't work and hence all my functionality of swiping gestures.
Can Someone please help and point out what I am doing wrong.
Here is my code.
protected void onServiceConnected() {
super.onServiceConnected();
FingerprintGestureController gestureController = getFingerprintGestureController();
Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable());
FingerprintGestureController.FingerprintGestureCallback callback = new
FingerprintGestureController.FingerprintGestureCallback() {
public void onGestureDetectionAvailabilityChanged(boolean available) {
super.onGestureDetectionAvailabilityChanged(available);
Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
}
public void onGestureDetected(int gesture) {
switch (gesture) {
case FINGERPRINT_GESTURE_SWIPE_UP:
scrollDown();
break;
case FINGERPRINT_GESTURE_SWIPE_DOWN:
scrollUp();
break;
case FINGERPRINT_GESTURE_SWIPE_RIGHT:
execute_swipe_right_functionality();
break;
case FINGERPRINT_GESTURE_SWIPE_LEFT:
execute_swipe_left_functionality();
break;
default:
Log.e("My Service",
"Error: Unknown gesture type detected!");
break;
}
}
};
gestureController.registerFingerprintGestureCallback(callback, new Handler());
}
I am working on a little app to check the signal strength of various network operators in my area. My current operators signal is quite unstable and I want to look into the strength of other GSM operators.
Sofar I've been using the TelephonyManager and a PhoneStateListener with the onSignalStrengthsChanged call back to get the GSM Signal strength of the current network operator, but it seems that this class only gives me info on the signal strength of the network attached to my SIM card.
I'm interested in measurement of GSM signal strength of ALL available operators. Searching the net has given vague hints on using internal android classes, but I've not yet found any good examples on this.
Any answer that can move me on to get a list of all available network operators and their signal strength are appreaciated.
Maybe these quotes and links can help you code your own solution:
1.- To get a list of available network providers (quoting How to get a list of available network providers? in full):
Since Android is open source I had a look at the sources and finally
found something called INetworkQueryService. I guess you can do the
same as the android settings implementation and interact with this
service. Some guidance through NetworkSettings.java:
onCreate starts the NetworkQueryService and binds it.
loadNetworksList() tells the service to query for network operators.
INetworkQueryServiceCallback is evaluated and if the event "EVENT_NETWORK_SCAN_COMPLETED" was raised, networksListLoaded will be
called to iterate over the available Networks.
2.- Even a quick read to NetworkSetting.java and INetworkQueryService interface, gives us an idea to achieve your goal.
Connect the service in declaration.
/**
* Service connection code for the NetworkQueryService.
* Handles the work of binding to a local object so that we can make
* the appropriate service calls.
*/
/** Local service interface */
private INetworkQueryService mNetworkQueryService = null;
/** Service connection */
private final ServiceConnection mNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) log("connection created, binding local service.");
mNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as it is bound, run a query.
loadNetworksList();
}
/** Handle the task of cleaning up the local binding */
public void onServiceDisconnected(ComponentName className) {
if (DBG) log("connection disconnected, cleaning local binding.");
mNetworkQueryService = null;
}
};
onCreate starts the NetworkQueryService and binds it.
Intent intent = new Intent(this, NetworkQueryService.class);
...
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), mNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
loadNetworksList() tells the service to query for network operators.
private void loadNetworksList() {
...
// delegate query request to the service.
try {
mNetworkQueryService.startNetworkQuery(mCallback);
} catch (RemoteException e) {
}
displayEmptyNetworkList(false);
}
INetworkQueryServiceCallback is evaluated:
/**
* This implementation of INetworkQueryServiceCallback is used to receive
* callback notifications from the network query service.
*/
private final INetworkQueryServiceCallback mCallback = new INetworkQueryServiceCallback.Stub() {
/** place the message on the looper queue upon query completion. */
public void onQueryComplete(List<OperatorInfo> networkInfoArray, int status) {
if (DBG) log("notifying message loop of query completion.");
Message msg = mHandler.obtainMessage(EVENT_NETWORK_SCAN_COMPLETED,
status, 0, networkInfoArray);
msg.sendToTarget();
}
};
If the event "EVENT_NETWORK_SCAN_COMPLETED" was raised, networksListLoaded will be called to iterate over the available Networks.
private void networksListLoaded(List<OperatorInfo> result, int status) {
...
if (status != NetworkQueryService.QUERY_OK) {
...
displayNetworkQueryFailed(status);
displayEmptyNetworkList(true);
} else {
if (result != null){
displayEmptyNetworkList(false);
...
} else {
displayEmptyNetworkList(true);
}
}
}
I hope it helps. I think it's an interesting challenge so maybe I'll give it a try next time I have some spare time. Good luck!
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallForwardingIndicatorChanged(boolean cfi) {
super.onCallForwardingIndicatorChanged(cfi);
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
//checkInternetConnection();
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
break;
case TelephonyManager.CALL_STATE_RINGING:
callState = "Ringing (" + incomingNumber + ")";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callState = "Offhook";
break;
}
Log.i("Phone Stats", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
#Override
public void onCellLocationChanged(CellLocation location) {
String cellLocationString = location.toString();
super.onCellLocationChanged(location);
}
#Override
public void onDataActivity(int direction) {
String directionString = "none";
switch (direction) {
case TelephonyManager.DATA_ACTIVITY_IN:
directionString = "IN";
break;
case TelephonyManager.DATA_ACTIVITY_OUT:
directionString = "OUT";
break;
case TelephonyManager.DATA_ACTIVITY_INOUT:
directionString = "INOUT";
break;
case TelephonyManager.DATA_ACTIVITY_NONE:
directionString = "NONE";
break;
default:
directionString = "UNKNOWN: " + direction;
break;
}
Log.i("Phone Stats", "onDataActivity " + directionString);
super.onDataActivity(direction);
}
#Override
public void onDataConnectionStateChanged(int state,int networktype) {
String connectionState = "Unknown";
switch (state ) {
case TelephonyManager.DATA_CONNECTED :
connectionState = "Connected";
break;
case TelephonyManager.DATA_CONNECTING:
connectionState = "Connecting";
break;
case TelephonyManager.DATA_DISCONNECTED:
connectionState = "Disconnected";
break;
case TelephonyManager.DATA_SUSPENDED:
connectionState = "Suspended";
break;
default:
connectionState = "Unknown: " + state;
break;
}
super.onDataConnectionStateChanged(state);
Log.i("Phone Stats", "onDataConnectionStateChanged "
+ connectionState);
}
#Override
public void onMessageWaitingIndicatorChanged(boolean mwi) {
super.onMessageWaitingIndicatorChanged(mwi);
}
#Override
public void onServiceStateChanged(ServiceState serviceState) {
String serviceStateString = "UNKNOWN";
switch (serviceState.getState()) {
case ServiceState.STATE_IN_SERVICE:
serviceStateString = "IN SERVICE";
break;
case ServiceState.STATE_EMERGENCY_ONLY:
serviceStateString = "EMERGENCY ONLY";
break;
case ServiceState.STATE_OUT_OF_SERVICE:
serviceStateString = "OUT OF SERVICE";
break;
case ServiceState.STATE_POWER_OFF:
serviceStateString = "POWER OFF";
break;
default:
serviceStateString = "UNKNOWN";
break;
}
Log.i("Phone Stats", "onServiceStateChanged " + serviceStateString);
super.onServiceStateChanged(serviceState);
}
#Override
public void onSignalStrengthChanged(int asu) {
Log.i("Phone Stats", "onSignalStrengthChanged " + asu);
setSignalLevel( asu);
super.onSignalStrengthChanged(asu);
}
private void setSignalLevel(int level) {
int sLevel = (int) ((level / 31.0) * 100);
Log.i("signalLevel ", "" + sLevel);
}
};
As I have no 50 reputation points, here is the result of my searches about the subject :
The solution of Alejandro Colorado seems to be the good one. But the problem is that the classes used to achieve it are reserved for android system applications, i.e. apps which are signed with the same signature key as the system.
How could it be possible? I found two way to achieve this.
The first one is to ask a job at any manufacturer compagny, and sign their NDA, but eh, that's not a really good solution. Especially as the app implemented and signed with this key will only work on devices from the compagny...
The second one, much more enjoyable, but i warn you, it's not gonna be easy, is to make your own ROM. You'll have to create your application, insert it in the /system/app/ directory of your ROM and recompile it to flash your device with your new system. But there's a question i've not answered yet, is the problem of unrecognised ROM signature.
I think the best way to avoid this problem is to add your ROM signing key in the Recovery you'll be using.
That's where i am at this point, maybe you could find these research usefull, i hope so!
I'll come back later if i find some more informations for you guys. Bye.
create a PhoneStateListener and handle the onSignalStrengthChanged callback. When your app is initialized, it should give you an initial notification. This is in 1.x. in 2.x, there's an open issueabout this.
I'm making an app that takes commands from User and write it in real time. What would be the Best option for me to take? Third Party software like sphinx or should I use the built in (android speech recognition)?
Secondly I want it to write in real time, like when I speak it starts writing?
You should use the built in Android Speech recognition. Specifically, you will need to operate the SpeechRecognier API so that there is no popup dialog box.
Also, do not expect SpeechRecognizer to return anything within onPartialResults(). It rarely does.
You could try to use Sphinx, but it seems other developers have difficulty getting it to run on Android. That said, sphinx will be your only option if you want your app to run without an internet connection.
Here is a snipped of code you will need to use SpeechRecognizer:
public void recognizeDirectly(Intent recognizerIntent)
{
// SpeechRecognizer requires EXTRA_CALLING_PACKAGE, so add if it's not
// here
if (!recognizerIntent.hasExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE))
{
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.dummy");
}
SpeechRecognizer recognizer = getSpeechRecognizer();
recognizer.startListening(recognizerIntent);
}
#Override
public void onResults(Bundle results)
{
Log.d(TAG, "full results");
receiveResults(results);
}
#Override
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "partial results");
receiveResults(partialResults);
}
/**
* common method to process any results bundle from {#link SpeechRecognizer}
*/
private void receiveResults(Bundle results)
{
if ((results != null)
&& results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION))
{
List<String> heard =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
float[] scores =
results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
receiveWhatWasHeard(heard, scores);
}
}
#Override
public void onError(int errorCode)
{
recognitionFailure(errorCode);
}
/**
* stop the speech recognizer
*/
#Override
protected void onPause()
{
if (getSpeechRecognizer() != null)
{
getSpeechRecognizer().stopListening();
getSpeechRecognizer().cancel();
getSpeechRecognizer().destroy();
}
super.onPause();
}
/**
* lazy initialize the speech recognizer
*/
private SpeechRecognizer getSpeechRecognizer()
{
if (recognizer == null)
{
recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(this);
}
return recognizer;
}
// other unused methods from RecognitionListener...
#Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "ready for speech " + params);
}
#Override
public void onEndOfSpeech()
{
}
gregm is right but the main "write in real time" part of the question wasn't answered. You need to add an extra to indicate that you are interested in getting parts of the result back:
Adding the extra to the intent works for me
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
Warning: Partial does not return only new stuff but also the previous one. So you need to implement a check for the differences by yourself...