I was wondering, how can I keep the device listening for voice commands with voice recognition while the device is asleep? The idea I have is that I would like the device to respond to my voice, even if I have the screen locked or the screen has timed out.
Is this possible? I have tried using this as a service and an interface and it stops listening once the screen locks. Can I receive any help with this? This is my class.
public class VoiceEngineService extends Activity {
private boolean isSpeakingDone = false; // default setting
private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
private AudioManager mAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_for_speech);
// mute beep sound
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
sr.setRecognitionListener(new listener());
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
.getClass().getName());
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
sr.startListening(i);
}
class listener implements RecognitionListener {
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
if (SharedPref.getVoiceController() == false) {
sr.cancel();
Intent i = new Intent();
sr.startListening(i);
} else {
sr.stopListening();
sr.destroy();
finish();
}
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
isSpeakingDone = true;
ArrayList<String> mDataList = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Intent i = new Intent();
i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
setResult(RESULT_OK, i);
finish();
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
} // end listener class
#Override
protected void onPause() {
if ((isSpeakingDone == false)) {
finish();
}
super.onPause();
}
#Override
protected void onStop() {
// when speaking is true finish() has already been called
if (isSpeakingDone == false) {
finish();
}
super.onStop();
}
#Override
protected void onDestroy() {
sr.stopListening();
sr.destroy();
super.onDestroy();
}
}
You have to implement your voice listener in a service. Create a class that extends 'Service' and make up some logic to take care of recording.
If you tried already the service, then it might be that you tried to redirect commands to an activity which most likely has been stopped by Android OS. Generally when talking about doing stuff when phone is in lock mode, you only can hope to accomplish tasks in one or more services coupled togethe.
When you are in Activity, of course wen the activity goes out of scope it will be shut down by Android OS. but services can still run in background unless shut dow mm explicitly by your own code or in rare cases that Android will recognize that it needs memory and processor power for other tasks.
Related
I am trying to show flurry interstitial but getting following message in debug screen and I am not receiving interstitial on my screen.
07-14 15:55:31.390: W/webview(10588): java.lang.Throwable: Warning: A
WebView method was called on thread 'FlurryAgent'. All WebView methods
must be called on the UI thread. Future versions of WebView may not
support use on other threads.
I have followed this tutorial completely :
Android Integration
At present I am working on AndEngine. As error replying I put my all code in UI thread but result is same.
Here is my code for displaying ads :
protected void onCreate(Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
// configure Flurry
FlurryAgent.setLogEnabled(false);
// init Flurry
FlurryAgent.init(MainGameActivity.this, MY_FLURRY_APIKEY);
mFlurryAdInterstitial = new FlurryAdInterstitial(MainGameActivity.this,
MY_ADSPACE_NAME);
FlurryAdTargeting adTargeting = new FlurryAdTargeting();
// enable test mode for this interstitial ad unit
adTargeting.setEnableTestAds(true);
mFlurryAdInterstitial.setTargeting(adTargeting);
// allow us to get callbacks for ad events
mFlurryAdInterstitial.setListener(interstitialAdListener);
mFlurryAdInterstitial.fetchAd();
}
FlurryAdInterstitialListener interstitialAdListener = new FlurryAdInterstitialListener() {
#Override
public void onFetched(final FlurryAdInterstitial adInterstitial) {
adInterstitial.displayAd();
}
#Override
public void onError(final FlurryAdInterstitial adInterstitial,
FlurryAdErrorType adErrorType, int errorCode) {
adInterstitial.destroy();
}
#Override
public void onAppExit(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClicked(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onClose(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDisplay(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onRendered(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
#Override
public void onVideoCompleted(FlurryAdInterstitial arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(MainGameActivity.this);
}
#Override
protected void onStop() {
super.onStop();
FlurryAgent.onEndSession(MainGameActivity.this);
}
#Override
public void onDestroy() {
super.onDestroy();
mFlurryAdInterstitial.destroy();
}
So what can I do in this situation?
I know there are quite a few posts about this already and I have read pretty much all of them (or so it feels at least). Yet my service refuses to work, at all.
In short: I have a widget that should launch the speecrecognizer service when clicked on. The service should then listen to input and (for now) print it out to logcat.
The service starts correctly (the logs in oncreate and ondestroy are printed) but the actual voice recognition part doesn't work.
I have added the service in the android manifest and added the "android.permission.RECORD_AUDIO" permission.
Part of the issue is that
SpeechRecognizer.isRecognitionAvailable(this)
returns false.
Here's the important bit of code from the service:
#Override
public void onCreate() {
super.onCreate();
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mListener);
if (SpeechRecognizer.isRecognitionAvailable(this))
Log.d(TAG, "we are go for speech recognition");
else
Log.d(TAG, "red light for speech recognition :c");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "i'm being destroyed :c");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "we made it");
playTtsForMessage("We made it", true);
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mSpeechRecognizer.startListening(speechIntent);
Log.d(TAG, "started listening");
return super.onStartCommand(intent, flags, startId);
}
private void broadcastStopIntent() {
Intent intent = new Intent(Intents.ACTION_VOICE_COMMANDS_STOP);
AddApplication.getInstance().sendBroadcast(intent, null);
}
All the log outputs get printed, apart from the ones i've placed inside the listener.
If interested, here's the listener:
private RecognitionListener mListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.d(TAG, "speech started");
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.d(TAG, "speech ended");
broadcastStopIntent();
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
ArrayList<String> spoken = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String msg = "";
for (String s : spoken) {
msg += s + " ";
}
Log.d(TAG, "what i understood from that was: " + msg);
WidgetVoiceCommandProviderIntentService.this.stopSelf();
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
};
I tried this guy's workaround: Android Speech Recognition as a service on Android 4.1 & 4.2
Which yielded no results. Any suggestions?
I've a viewpager in my application. When the user swipes to the right/left I use TTS engine to speak the text and MediaPlayer to play a sound.
The problem is both plays simultaneously.. How do I play the sound once TTS engine speaks the text is over?
P.S: I don't want to use sleep or wait.
Update:
Here is my code:
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
} else {
//Do Something here
}
if(Build.VERSION.SDK_INT >= 15 ){
UtteranceProgressListener listener = new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
// TODO Auto-generated method stub
}
#Override
public void onError(String utteranceId) {
// TODO Auto-generated method stub
}
#Override
public void onDone(String utteranceId) {
// TODO Auto-generated method stub
//start MediaPlayer
playMedia(viewPager.getCurrentItem());
}
};
tts.setOnUtteranceProgressListener(listener);
}
else{
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){
#Override
public void onUtteranceCompleted(String arg0) {
playMedia(viewPager.getCurrentItem());
}
});
}
} else {
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
You have to give it an id param. Otherwise it doesn't call the listeners:
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
textToSpeech.speak(string,TextToSpeech.QUEUE_ADD, params);
In android their are two APIs to detect if the TTS engine finished speaking:
> Android 4 (ICS)
UtteranceProgressListener listener = new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
// TODO Auto-generated method stub
}
#Override
public void onError(String utteranceId) {
// TODO Auto-generated method stub
}
#Override
public void onDone(String utteranceId) {
// TODO Auto-generated method stub
//start MediaPlayer
}
};
yourTTSObject.setOnUtteranceProgressListener(listener);
Perior to ICS Android 4.0 you can use :
yourTTSObject.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){
#Override
public void onUtteranceCompleted(String arg0) {
// start your mediaplayer here
}
});
take a look at the documentations here
I want to send data from one application A to other application B using AIDL file. My appl A is like below,
public class LibValue {
public static native int intFromJNI(int n);
static {
System.loadLibrary("hello");
System.out.println("LibValue : Loading library");
}
I am getting value from JNI file to above class. The data from above class to send another app B using AIDL service is like below.
IEventService.aidl
interface IEventService {
int intFromJNI(in int n);
}
for this I written IEventImpl.java class
public class IEventImpl extends IEventService.Stub{
int result;
#Override
public int intFromJNI(int n) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("IEventImpl"+LibValue.intFromJNI(n));
return LibValue.intFromJNI(n);
}
}
To access above class I writtn service class like below
public class EventService extends Service {
public IEventImpl iservice;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("EventService", "indside onBind");
return this.iservice;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.iservice = new IEventImpl();
Log.i("EventService", "indside OncREATE");
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
this.iservice = null;
super.onDestroy();
}
All above classes are server side. The below class is Client(app) class to access data.
public class EventClient extends Activity implements OnClickListener, ServiceConnection{
public IEventService myService;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_client);
button = (Button)findViewById(R.id.button1);
this.button.setOnClickListener(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (!super.bindService(new Intent(IEventService.class.getName()),
this, BIND_AUTO_CREATE)) {
Log.w("EventClient", "Failed to bind to service");
System.out.println("inside on resume");
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
super.unbindService(this);
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
this.myService = IEventService.Stub.asInterface(service);
Log.i("EventClient", "ServiceConnected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.d("EventClient", "onServiceDisconnected()'ed to " + name);
// our IFibonacciService service is no longer connected
this.myService = null;
}
I am trying to access data from service class but not able to find out the way. can anybody tell how to access data from service to client application ?
Thanks
I'm using the TextToSpeech API in my code and it doesn't work when I try to call .speak() function from OnStart(), however it works when I call it from a button onClickListener(). Any idea why? Thank you.
public class TtsDemoActivity extends Activity {
private TextToSpeech mTts;
private OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
PlaySound();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttsdemo);
// Initialize Text To speech
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
});
Button myButton = (Button)findViewById(R.id.buttontts1);
myButton.setOnClickListener(buttonListener);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
PlaySound();
}
protected void PlaySound()
{
String word = "Hello world";
mTts.speak(word, TextToSpeech.QUEUE_FLUSH, null);
}
You must wait until the TTS subsystem signals that it is ready: if it isn't ready when onStart is called, it will fail. If you are trying to speak as soon as it is ready call PlaySound from inside the OnInitListener:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttsdemo);
// Initialize Text To speech
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int arg0) {
if(arg0 == TextToSpeech.SUCCESS) PlaySound();
}
});
Button myButton = (Button)findViewById(R.id.buttontts1);
myButton.setOnClickListener(buttonListener);
}
Maybe I am blind, but in your onStart method, you aren't ever calling
PlaySound()
Like you are in
#Override
public void onClick(View arg0) {
PlaySound();
}