Code example for TextToSpeech

Methods: isLanguageAvailable, playSilence, setLanguage, setOnUtteranceCompletedListener, setSpeechRate, sleep, speak

0
 
			HashMap<String, String> params = new HashMap<String, String>();
			params.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
					String.valueOf(AudioManager.STREAM_MUSIC));
 
			tts.speak(intro, TextToSpeech.QUEUE_FLUSH, params);
			tts.playSilence(500, TextToSpeech.QUEUE_ADD, params);
 
			if (from != null)
			{ 
 
				tts.speak(from, TextToSpeech.QUEUE_ADD, params);
				tts.playSilence(500, TextToSpeech.QUEUE_ADD, params);
			} 
			params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, intro + from + msg);
			tts.speak(sms.getDisplayMessageBody(), TextToSpeech.QUEUE_ADD, params);
		} 
 
	} 
 
	public SpeechService() 
	{ 
 
	} 
 
	@Override 
	public void onCreate() 
	{ 
		super.onCreate(); 
 
		context = this.getApplicationContext();
		resolver = context.getContentResolver();
 
	} 
 
	@Override 
	public IBinder onBind(Intent arg0)
	{ 
		// TODO Auto-generated method stub 
		return null; 
	} 
 
	@Override 
	public int onStartCommand(Intent intent, int flags, int startId)
	{ 
 
		if (intent == null)
		{ 
			stopSelf();
			return super.onStartCommand(intent, flags, startId);
		} 
		Bundle pudsBundle = intent.getExtras();
 
		Object[] pdus = (Object[]) pudsBundle.get("pdus");
		sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
		Log.i(TAG, sms.toString());
 
		if (mTts == null)
		{ 
			mTts = new TextToSpeech(context.getApplicationContext(), this);
		} 
 
		String intro = "Incoming message";
		String from = sms.getDisplayOriginatingAddress();
		if (from != null)
		{ 
 
			Cursor c = null;
			try 
			{ 
				Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(from));
 
				c = resolver
						.query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
 
				if (c.getCount() > 0)
				{ 
					c.moveToFirst();
 
					from = c.getString(0);
				} 
			} finally 
			{ 
				if (c != null)
				{ 
					c.close();
				} 
			} 
 
			intro += " from";
		} 
 
		messages.offer(new Message(intro, from, sms.getDisplayMessageBody()));
 
		return super.onStartCommand(intent, flags, startId);
	} 
 
	@SuppressWarnings("deprecation") 
	@Override 
	public void onInit(int arg0)
	{ 
		mTts.setOnUtteranceCompletedListener(this);
 
		Locale l = Locale.getDefault();
		int result = mTts.isLanguageAvailable(l);
		if (result == TextToSpeech.LANG_AVAILABLE || result == TextToSpeech.LANG_COUNTRY_AVAILABLE
				|| result == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)
		{ 
 
			mTts.setLanguage(l);
		} else 
		{ 
			Toast.makeText(context, "Lang not available:" + result, Toast.LENGTH_LONG).show();
		} 
 
		mTts.setSpeechRate(0.85f);
		playNext(); 
	} 
 
	@Override 
	public void onDestroy() 
	{ 
		super.onDestroy(); 
 
		if (mTts != null)
		{ 
			mTts.shutdown();
		} 
	} 
 
	@Override 
	public void onUtteranceCompleted(String arg0)
	{ 
		playNext(); 
	} 
 
	private void playNext() 
	{ 
		Message m = messages.poll();
		if (m == null)
		{ 
			try 
			{ 
				Thread.sleep(1000);
			} catch (InterruptedException e)
			{ 
				Log.e(TAG, "SpeechService:onUtteranceCompleted " + e);
				e.printStackTrace();
			}