I持つAPIのデモで使用されるこのクラス:私は、その後のonClickボタンで別のクラスを持っているし、使用してsayHello();
メソッドを呼び出すテキスト読み上げのAPI - ** - ヘルプしてください - ** - 呼び出す方法
public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
static int currentHelloIndex = 0;
private static final String TAG = "TextToSpeechDemo";
public static TextToSpeech mTts;
private Button mAgainButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_to_speech);
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
mTts = new TextToSpeech(this,
this // TextToSpeech.OnInitListener
);
// The button is disabled in the layout.
// It will be enabled upon initialization of the TTS engine.
mAgainButton = (Button) findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sayHello();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
// Implements TextToSpeech.OnInitListener.
@Override
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
// Note that a language may not be available, and the result will indicate this.
int result = mTts.setLanguage(Locale.US);
// Try this someday for some interesting results.
// int result mTts.setLanguage(Locale.FRANCE);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user.
sayHello();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
private static final String[] HELLOS = {
"What is a string of words that satisfy the grammatical rules of a sentence",
"Salutations",
"Greetings",
"Howdy",
"What's crack-a-lackin?",
"That explains the stench!"
};
public static void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[currentHelloIndex];
currentHelloIndex = (currentHelloIndex + 1) % helloLength;
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}
}
TextToSpeech.sayHello();
これは私にプロセスクローズドエラーを与え、logcatソースのヌル例外エラーが見つかりませんでした。誰かがこの問題を助けてくれますか?ありがとう
他のクラスからSayHelloメソッドを呼び出す方法はありません。 – Tommy
助けてくれてありがとう、私はmTts = main.mTtsのような主なアクティビティから変数を引き出し、mainからcurrentHelloIndexを呼び出して新しいintに格納する変数を作成することでこれを行うことができました。ご協力いただきありがとうございます。 – Tommy