Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 26 | ||
|
||
|
||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 26 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:26.1.0' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.java.ttslibrary" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package com.java.ttslibrary; | ||
|
||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.app.Activity; | ||
|
||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
|
||
import android.speech.tts.TextToSpeech; | ||
import android.util.Log; | ||
import android.media.AudioManager; | ||
import java.util.Locale; | ||
|
||
public class TTSLib { | ||
static TextToSpeech tts; | ||
static Locale _locale = Locale.JAPANESE; | ||
static String packagetouse = "com.google.android.tts"; | ||
|
||
static Bundle bundle = null; | ||
static HashMap<String, String> param; | ||
static Locale.Builder builder = new Locale.Builder(); | ||
|
||
public static boolean IsRunning(){ | ||
return (tts!=null); | ||
} | ||
|
||
public static void SetPitch(float pitch){ | ||
if(tts!=null){ | ||
tts.setPitch(pitch); | ||
} | ||
} | ||
|
||
public static void SetSpeedRate(float speechrate){ | ||
if(tts!=null){ | ||
tts.setSpeechRate(speechrate); | ||
} | ||
} | ||
|
||
public static boolean SetLocale(String locale, String script, String region){ | ||
Locale locale_ = builder.setLanguage(locale).setScript(script).setRegion(region).build(); | ||
if(tts==null){ | ||
return false; | ||
} | ||
int result = tts.setLanguage(locale_); | ||
if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) { | ||
return false; | ||
} | ||
_locale = locale_; | ||
return true; | ||
} | ||
|
||
public static String GetLocale(){ | ||
return (tts!=null) ? tts.getLanguage().toString() : ""; | ||
} | ||
|
||
public static String[] GetAvailableLocales(){ | ||
|
||
/*if(instance.tts!=null){ | ||
Set<Locale> locales = instance.tts.getAvailableLanguages(); | ||
String[] temp = new String[locales.size()]; | ||
Locale[] _locales = locales.toArray(new Locale[0]); | ||
for(int i=0;i<locales.size();i++){ | ||
temp[i]=_locales[i].toString(); | ||
} | ||
return temp; | ||
} | ||
return new String[]{};*/ | ||
Locale[] locales = Locale.getAvailableLocales(); | ||
String[] temp = new String[locales.length]; | ||
|
||
for(int i=0;i<locales.length;i++){ | ||
temp[i] = locales[i].toString(); | ||
} | ||
|
||
return temp; | ||
} | ||
|
||
public static boolean setEngineByPackageName(String packagename){ | ||
|
||
if(tts!=null && tts.setEngineByPackageName(packagename)==TextToSpeech.SUCCESS){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
public static void StartTTS(final Activity activity) | ||
{ | ||
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); | ||
|
||
tts = new TextToSpeech(activity.getApplicationContext(), new TextToSpeech.OnInitListener() { | ||
@Override | ||
public void onInit(int status) { | ||
if (status == TextToSpeech.SUCCESS) { | ||
|
||
int result = tts.setLanguage(Locale.JAPANESE); | ||
if (result == TextToSpeech.LANG_MISSING_DATA | ||
|| result == TextToSpeech.LANG_NOT_SUPPORTED) { | ||
Log.e("error", "Language Not supported"); | ||
DownloadTTSData(activity); | ||
} | ||
else{ | ||
Log.v("TTS","onInit succeeded"); | ||
} | ||
} else { | ||
Log.e("error","Initialization Failed"); | ||
} | ||
|
||
} | ||
},packagetouse); | ||
} | ||
|
||
public static void DownloadTTSData(Activity activity) { | ||
Intent installTTSIntent = new Intent(); | ||
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); | ||
activity.startActivity(installTTSIntent); | ||
} | ||
|
||
public static boolean IsUttering() { | ||
if(tts!=null && tts.isSpeaking()){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static void StopUtterance(){ | ||
if(tts!=null){ | ||
tts.stop(); | ||
} | ||
} | ||
|
||
public static boolean GetLanguageAvailability(Activity activity, String locale) { | ||
int result = tts.isLanguageAvailable(builder.setLanguage(locale).build()); | ||
|
||
if(!(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)) { | ||
return true; | ||
} | ||
else { | ||
//DownloadTTSData(activity); | ||
return false; | ||
} | ||
} | ||
|
||
public static void Speak(String s){ | ||
|
||
//Log.e("error","SpeakJapanese Is Running normally: " + tts.toString()); | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
//Log.v("Change to new API", "Speak new API"); | ||
if(bundle==null){ | ||
Bundle bundle = new Bundle(); | ||
bundle.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC); | ||
} | ||
tts.speak(s, TextToSpeech.QUEUE_FLUSH, bundle, null); | ||
} else { | ||
//Log.v(s, "Speak old API"); | ||
if(param==null){ | ||
param = new HashMap<>(); | ||
param.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC)); | ||
} | ||
|
||
tts.speak(s, TextToSpeech.QUEUE_FLUSH, param); | ||
} | ||
} | ||
|
||
static protected void onDestroy() { | ||
// Don't forget to shutdown tts! | ||
if (tts != null) { | ||
Log.v("onDestroy()","onDestroy: shutdown TTS"); | ||
tts.stop(); | ||
tts.shutdown(); | ||
} | ||
} | ||
|
||
public static void StopTTS() { | ||
onDestroy(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">TTSLibrary</string> | ||
</resources> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 26 | ||
defaultConfig { | ||
applicationId "com.jongamedev.ttstest" | ||
minSdkVersion 21 | ||
targetSdkVersion 26 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:26.1.0' | ||
} |