Created
September 25, 2015 23:19
-
-
Save dyadica/746558fa308a9deddfc5 to your computer and use it in GitHub Desktop.
Simple and basic Android TTS all ready to be turned into a unity plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.dyadica.ttsbasic; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Build; | |
import android.speech.RecognizerIntent; | |
import android.speech.tts.TextToSpeech; | |
import android.speech.tts.UtteranceProgressListener; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.Locale; | |
public class TTSActivity extends AppCompatActivity | |
{ | |
public TextToSpeech talker; | |
public static Locale vocLocale = Locale.UK; | |
public static float vocPitch = 1f; | |
public static float vocRate = 1f; | |
int MY_DATA_CHECK_CODE = 10; | |
Button b1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_tts); | |
CheckAndGo(); | |
b1 = (Button)findViewById(R.id.button); | |
b1.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
SpeakText("Hello"); | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() | |
{ | |
if(talker != null) | |
{ | |
talker.stop(); | |
talker.shutdown(); | |
} | |
super.onDestroy(); | |
} | |
@Override | |
protected void onPause() | |
{ | |
if(talker != null) | |
{ | |
talker.stop(); | |
} | |
super.onPause(); | |
} | |
@Override | |
protected void onResume() | |
{ | |
if(talker == null) | |
{ | |
InitialiseSpeaker(); | |
} | |
super.onResume(); | |
} | |
public void CheckAndGo() | |
{ | |
Intent checkIntent = new Intent(); | |
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); | |
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); | |
} | |
public void InitialiseSpeaker() | |
{ | |
talker = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() | |
{ | |
@Override | |
public void onInit(int status) | |
{ | |
if(status != TextToSpeech.ERROR) | |
{ | |
talker.setLanguage(vocLocale); | |
talker.setPitch(vocPitch); | |
talker.setSpeechRate(vocRate); | |
} | |
} | |
}); | |
} | |
public boolean isSpeaking() | |
{ | |
return talker.isSpeaking(); | |
} | |
public void SpeakText(String toSpeak) | |
{ | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) | |
{ | |
talker.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, null); | |
} | |
else | |
{ | |
talker.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); | |
} | |
} | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
{ | |
if (requestCode == MY_DATA_CHECK_CODE) | |
{ | |
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) | |
{ | |
// success, create the TTS instance | |
InitialiseSpeaker(); | |
} | |
else | |
{ | |
// missing data, install it | |
Intent installIntent = new Intent(); | |
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); | |
startActivity(installIntent); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment