Skip to content

Instantly share code, notes, and snippets.

Created March 4, 2016 00:09
Show Gist options
  • Save anonymous/47efc9c1ca08d808e0be to your computer and use it in GitHub Desktop.
Save anonymous/47efc9c1ca08d808e0be to your computer and use it in GitHub Desktop.
//THE PROBLEM IS THAT hypothesis.getHypstr(); IS ALWAYS "ORANGES AND RAIBOWS", EVEN AFTER I CHANGE USERS KEYWORD!!!
import android.Manifest;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.NotificationCompat;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import static edu.cmu.pocketsphinx.SpeechRecognizerSetup.defaultSetup;
import edu.cmu.pocketsphinx.SpeechRecognizer;
public class MyService extends Service implements RecognitionListener {
ContentObserver mSettingsContentObserver;
private static final String TAG = "myTag";
SpeechRecognizer mSpeechRecognizer;
FileOutputStream outputStream;
SharedPreferences keyword;
String enteredKeyword;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
}
@Override
public void onCreate() {
super.onCreate();
keyword = getSharedPreferences("keyword", Context.MODE_PRIVATE);
Log.v(TAG, "Listening for " + keyword.getString("keyword", "oranges and rainbows") + " IN SERVICE");
//Register voice recog listener :)
try {
Assets assets = new Assets(MyService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
Log.v(TAG, "SET UP DIRECTORIES!");
mSpeechRecognizer.startListening("usersKeyword");
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, e.toString());
}
}
public void setupRecognizer(File sphinxDir) {
try {
mSpeechRecognizer = defaultSetup()
.setAcousticModel(new File(sphinxDir, "en-us-ptm"))
.setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
.setBoolean("-allphone_ci", true)
.setKeywordThreshold(1e-40f)
.getRecognizer();
} catch (IOException e) {
e.printStackTrace();
}
mSpeechRecognizer.addListener(this);
mSpeechRecognizer.addKeyphraseSearch("usersKeyword", keyword.getString("keyword", "oranges and rainbows"));
Log.v(TAG, "Added keyphrase search :)");
}
@Override
public void onBeginningOfSpeech() {
Log.v(TAG, "Started talking " );
}
@Override
public void onEndOfSpeech() {
Log.v(TAG, "Ended talking ");
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null) { //no one spoke
return;
}
String text = hypothesis.getHypstr();
Log.v(TAG, hypothesis.getHypstr()); //THIS IS ALWAYS ORANGES AND RAINBOWS FOR SOME REASON!!!!
if (text.equals(keyword.getString("keyword", "oranges and rainbows"))) {
Log.v(TAG, "Heard user keyword!"); //This log never comes when I change the keyword
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening("usersKeyword");
}
}
@Override
public void onResult(Hypothesis hypothesis) {
}
@Override
public void onError(Exception e) {
}
@Override
public void onTimeout() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment