Last active
October 11, 2015 20:38
-
-
Save kimukou/3915918 to your computer and use it in GitHub Desktop.
IS01_N2TTS_test
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
import android.speech.tts.TextToSpeech; | |
import com.google.tts.TextToSpeechBeta; | |
private int initTtsMode = -1; //-1:TTS無効、1:EN、2:N2TTS | |
private Resources m_r; | |
protected Resources m_r; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
m_r = getResources(); | |
} | |
private TextToSpeech tts_init() { | |
TextToSpeech mTts = null; | |
//2.1以下で、 | |
// Text-To-Speech Extended | |
// N2 TTS | |
// が入っている状態のとき | |
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1 | |
&& isCheck_N2Tts() && isCheck_GoogleTts()){ | |
TextToSpeechBeta.OnInitListener listner = | |
new TextToSpeechBeta.OnInitListener(){ | |
//TTS | |
@Override | |
public void onInit(int status, int version) { | |
if (status != TextToSpeech.SUCCESS)return; | |
int result = 0; | |
do{ | |
Locale locale = m_r.getConfiguration().locale;//Locale.getDefault(); | |
if(locale.equals(Locale.JAPAN) && isCheck_N2Tts()){ | |
result = mTts.setLanguage(Locale.JAPAN); | |
if (result == TextToSpeech.LANG_MISSING_DATA || | |
result == TextToSpeech.LANG_NOT_SUPPORTED) { | |
} | |
else{ | |
initTtsMode= 1; | |
break; | |
} | |
} | |
result = mTts.setLanguage(Locale.US); | |
if (result == TextToSpeech.LANG_MISSING_DATA || | |
result == TextToSpeech.LANG_NOT_SUPPORTED) { | |
Log.e(TAG, "Language is not available."); | |
break; | |
} | |
initTtsMode= 0; | |
}while(false); | |
} | |
}; | |
mTts = new TextToSpeechBeta( this,listner); | |
return mTts; | |
} | |
//通常のTTS | |
TextToSpeech.OnInitListener listner = | |
new TextToSpeech.OnInitListener(){ | |
//TTS | |
@Override | |
public void onInit(int status) { | |
if (status != TextToSpeech.SUCCESS)return; | |
int result = 0; | |
do{ | |
Locale locale = m_r.getConfiguration().locale;//Locale.getDefault(); | |
if(locale.equals(Locale.JAPAN) && isCheck_N2Tts()){ | |
result = mTts.setLanguage(Locale.JAPAN); | |
if (result == TextToSpeech.LANG_MISSING_DATA || | |
result == TextToSpeech.LANG_NOT_SUPPORTED) | |
{ | |
} | |
else{ | |
//女性の声の方が良い場合の指定方法 | |
//mTts.setLanguage(new Locale(Locale.JAPAN.getLanguage(), Locale.JAPAN.getCountry(), "F01")); | |
initTtsMode= 1; | |
break; | |
} | |
} | |
result = mTts.setLanguage(Locale.US); | |
if (result == TextToSpeech.LANG_MISSING_DATA || | |
result == TextToSpeech.LANG_NOT_SUPPORTED) { | |
Log.e(TAG, "Language is not available."); | |
break; | |
} | |
initTtsMode= 0; | |
}while(false); | |
} | |
}; | |
mTts = new TextToSpeech( this,listner); | |
return mTts; | |
} | |
//google_ttsが入っているか判定 | |
private boolean isCheck_GoogleTts() { | |
try{ | |
//パッケージ名を指定してインストール状況をチェック | |
PackageManager packageManager = this.getPackageManager(); | |
ApplicationInfo applicationInfo = packageManager.getApplicationInfo("com.google.tts",PackageManager.GET_META_DATA); | |
if(applicationInfo==null)return false; | |
} | |
catch(NameNotFoundException exception){ | |
return false; | |
} | |
return true; | |
} | |
//n2ttsが入っているか判定 | |
private boolean isCheck_N2Tts() { | |
try{ | |
//パッケージ名を指定してインストール状況をチェック | |
PackageManager packageManager = this.getPackageManager(); | |
ApplicationInfo applicationInfo = packageManager.getApplicationInfo("jp.kddilabs.n2tts",PackageManager.GET_META_DATA); | |
if(applicationInfo==null)return false; | |
} | |
catch(NameNotFoundException exception){ | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment