Back to project page umpire_buddy.
The source code is released under:
Apache License
If you think the Android project umpire_buddy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bluefairyapps.java.android.umpirebuddy.systems; /* w w w . j av a2s. c o m*/ import android.content.Context; import android.media.AudioManager; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.util.Log; import com.bluefairyapps.java.android.umpirebuddy.R; import com.bluefairyapps.java.android.umpirebuddy.enums.EventsEnum; import com.bluefairyapps.java.android.umpirebuddy.event.Event; import com.bluefairyapps.java.android.umpirebuddy.event.EventListener; public class AudioSystem implements EventListener { private static final String TAG = AudioSystem.class.getSimpleName(); private static final float TTS_PITCH = 0.5f; private static AudioSystem sInstance = null; private EventManager mEventManager; private Context mContext; private TextToSpeech mTts; private static boolean mEnabled = false; private boolean mTtsReady = false; public AudioSystem(Context context) { mContext = context; updateEnabled(); mEventManager = EventManager.get(); mEventManager.registerForEvent(this, EventsEnum.EVENT_AUDIO_STRIKE); mEventManager.registerForEvent(this, EventsEnum.EVENT_AUDIO_BALL); mEventManager.registerForEvent(this, EventsEnum.EVENT_BATTER_OUT); mEventManager.registerForEvent(this, EventsEnum.EVENT_BATTER_WALKS); mEventManager.registerForEvent(this, EventsEnum.EVENT_NEW_INNING); mEventManager.registerForEvent(this, EventsEnum.EVENT_NEW_GAME); mEventManager.registerForEvent(this, EventsEnum.EVENT_GAME_OVER); mEventManager.registerForEvent(this, EventsEnum.EVENT_TOGGLE_AUDIO); mTts = new TextToSpeech(mContext, new OnInitListener() { @Override public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { mTtsReady = true; mTts.setPitch(TTS_PITCH); } } }); } public static void init(Context context) { if(sInstance == null) { sInstance = new AudioSystem(context); } } public static AudioSystem get() { return sInstance; } public static boolean active() { return sInstance != null; } @Override public void onEvent(Event event) { Log.d(TAG, event.getType().name() + " received."); switch(event.getType()) { case EVENT_AUDIO_STRIKE: speakMessage(mContext.getString(R.string.tts_strike)); break; case EVENT_AUDIO_BALL: speakMessage(mContext.getString(R.string.tts_ball)); break; case EVENT_BATTER_OUT: speakMessage(mContext.getString(R.string.tts_batterOut)); break; case EVENT_BATTER_WALKS: speakMessage(mContext.getString(R.string.tts_batterWalks)); break; case EVENT_NEW_INNING: speakMessage(mContext.getString(R.string.tts_newInning)); break; case EVENT_NEW_GAME: newGame(); break; case EVENT_GAME_OVER: gameOver(); break; case EVENT_TOGGLE_AUDIO: updateEnabled(); break; default: Log.d(TAG, "Unable to handle received event."); } } private void newGame() { speakMessage(mContext.getString(R.string.tts_newGame)); } private void gameOver() { speakMessage(mContext.getString(R.string.tts_gameOver)); } private void speakMessage(String message) { int ringerMode = ((AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE)).getRingerMode(); if(mEnabled && mTtsReady && ringerMode == AudioManager.RINGER_MODE_NORMAL) { mTts.speak(message, TextToSpeech.QUEUE_ADD, null); } } private void updateEnabled() { mEnabled = mContext.getSharedPreferences(mContext.getString(R.string.pref_filename), Context.MODE_PRIVATE).getBoolean(mContext.getString(R.string.pref_audio_key), Boolean.valueOf(mContext.getString(R.string.pref_audio_dflt))); } public void finish() { mTts.shutdown(); mEventManager.unregister(this); mTtsReady = false; sInstance = null; } }