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

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();

}

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值