texttospeech android,Text to speech(TTS)-Android

package com.keshav.speechtotextexample;

import java.util.ArrayList;

import java.util.Locale;

import android.app.Activity;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.os.Bundle;

import android.speech.RecognizerIntent;

import android.view.Menu;

import android.view.View;

import android.widget.ImageButton;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private TextView txtSpeechInput;

private ImageButton btnSpeak;

private final int REQ_CODE_SPEECH_INPUT = 100;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);

btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

// hide the action bar

getActionBar().hide();

btnSpeak.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

promptSpeechInput();

}

});

}

/**

* Showing google speech input dialog

* */

private void promptSpeechInput() {

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,

getString(R.string.speech_prompt));

try {

startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);

} catch (ActivityNotFoundException a) {

Toast.makeText(getApplicationContext(),

getString(R.string.speech_not_supported),

Toast.LENGTH_SHORT).show();

}

}

/**

* Receiving speech input

* */

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case REQ_CODE_SPEECH_INPUT: {

if (resultCode == RESULT_OK && null != data) {

ArrayList result = data

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

txtSpeechInput.setText(result.get(0));

}

break;

}

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

====================================================

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_gradient"

android:orientation="vertical">

android:id="@+id/txtSpeechInput"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="100dp"

android:textColor="@color/white"

android:textSize="26dp"

android:textStyle="normal" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="60dp"

android:gravity="center"

android:orientation="vertical">

android:id="@+id/btnSpeak"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"

android:src="@drawable/ico_mic" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="@string/tap_on_mic"

android:textColor="@color/white"

android:textSize="15dp"

android:textStyle="normal" />

===============================================================

Speech To Text

Settings

Hello world!

Say something…

Sorry! Your device doesn\'t support speech input

Tap on mic to speak

===============================================================

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值