Back to project page speakerbox.
The source code is released under:
Apache License
If you think the Android project speakerbox listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Mapzen/*from w w w . j a va 2 s.co m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mapzen.speakerbox; import android.app.Activity; import android.app.Application; import android.media.AudioManager; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import java.util.ArrayList; import java.util.LinkedHashMap; public class Speakerbox implements TextToSpeech.OnInitListener { final static String TAG = Speakerbox.class.getSimpleName(); final Activity activity; final TextToSpeech textToSpeech; final Application.ActivityLifecycleCallbacks callbacks; private boolean initialized = false; private boolean muted = false; private String playOnInit = null; private int queueMode = TextToSpeech.QUEUE_FLUSH; private final LinkedHashMap<String, String> samples = new LinkedHashMap<String, String>(); private final ArrayList<String> unwantedPhrases = new ArrayList<String>(); public Speakerbox(Activity activity) { this.activity = activity; this.textToSpeech = new TextToSpeech(activity, this); final Application application = (Application) activity.getApplicationContext(); this.callbacks = new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { if (Speakerbox.this.activity == activity) { textToSpeech.shutdown(); application.unregisterActivityLifecycleCallbacks(this); } } }; application.registerActivityLifecycleCallbacks(callbacks); enableVolumeControl(); } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { initialized = true; if (playOnInit != null) { playInternal(playOnInit); } } else { Log.e(TAG, "Initialization failed."); } } public void play(CharSequence text) { play(text.toString()); } public void play(String text) { if(doesNotContainUnwantedPhrase(text)) { text = applyRemixes(text); if (initialized) { playInternal(text); } else { playOnInit = text; } } } public void stop() { textToSpeech.stop(); } private String applyRemixes(String text) { for (String key : samples.keySet()) { if (text.contains(key)) { text = text.replace(key, samples.get(key)); } } return text; } private void playInternal(String text) { if (!muted) { Log.d(TAG, "Playing: \""+ text + "\""); textToSpeech.speak(text, queueMode, null); } } public void dontPlayIfContains(String text) { unwantedPhrases.add(text); } private boolean doesNotContainUnwantedPhrase(String text){ for(String invalid : unwantedPhrases) { if(text.contains(invalid)) { return false; } } return true; } public void mute() { muted = true; } public void unmute() { muted = false; } public boolean isMuted() { return muted; } public void remix(String original, String remix) { samples.put(original, remix); } public TextToSpeech getTextToSpeech() { return textToSpeech; } public void enableVolumeControl() { activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); } public void disableVolumeControl() { activity.setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE); } public void setQueueMode(int queueMode) { this.queueMode = queueMode; } }