当前位置: 首页 > 工具软件 > TextToSpeech > 使用案例 >

java text to speech_Java TextToSpeech.speak方法代码示例

束福
2023-12-01

import android.speech.tts.TextToSpeech; //导入方法依赖的package包/类

@Override

protected void onCreate(Bundle savedInstanceState) {

handler = new Handler();

// Set the resources locale

String languageExtra = getIntent().getStringExtra(SupportedLanguage.class.getSimpleName());

final SupportedLanguage language = languageExtra == null ? SupportedLanguage.ENGLISH : SupportedLanguage.valueOf(languageExtra);

LocaleUtils.setResourcesLocale(language.getLocale(), this);

// Load configuration

InputStream inputStream = null;

try {

inputStream = getBaseContext().getAssets().open("application.properties");

applicationProperties.load(inputStream);

} catch (IOException e) {

Log.e(TAG, "Unable to load application.properties", e);

} finally {

IOUtils.closeQuietly(inputStream);

}

// Get the cloudSightService

String cloudSightApiKey = applicationProperties.getProperty("cloudSight.apiKey");

cloudSightService = new CloudSightServiceImpl(cloudSightApiKey);

// Initialize the activity content

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_photo_analysis);

// Display the photo

final byte[] photo = getIntent().getByteArrayExtra(INTENT_PHOTO_EXTRA);

ImageView photoImageView = (ImageView) findViewById(R.id.photoImageView);

photoImageView.setImageBitmap(BitmapFactory.decodeByteArray(photo, 0, photo.length));

// Load the TTS engine

textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

@Override

public void onInit(int status) {

if (status == TextToSpeech.ERROR) {

Toast.makeText(PhotoAnalysisActivity.this, R.string.error_tts_engine, Toast.LENGTH_SHORT).show();

} else {

textToSpeech.setLanguage(language.getLocale());

// Send the photo to Cloud Sight API to analyze the photo

Toast.makeText(PhotoAnalysisActivity.this, R.string.analyze_photo, Toast.LENGTH_SHORT).show();

textToSpeech.speak(getResources().getString(R.string.analyze_photo), TextToSpeech.QUEUE_ADD, null);

new AnalyzePhotoTask(photo, language).execute();

}

}

});

}

 类似资料: