我想问一下纠正我的问题。我看到一篇文章教会如何使用文本到语音安卓代码,但我遵循代码,我的AVD似乎没有响应。所以我去了另一个代码并输入,avd似乎也没有响应。有人可以帮我检查错误吗?
package net.learn2develop.tts;
import java.util.Locale;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements OnInitListener{
private int MY_DATA_CHECK_CODE=0;
private TextToSpeech tts;
private EditText inputText;
private Button speakButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText=(EditText)findViewById(R.id.editText1);
tts=new TextToSpeech(getApplicationContext(),
new TextToSpeech.OnInitListener(){
@Override
public void onInit(int status){
if(status!=TextToSpeech.ERROR){
tts.setLanguage(Locale.UK);}
}
});
}
@Override
public void onPause(){
if(tts!=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void speakText(View view){
String toSpeak=inputText.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
tts.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.learn2develop.tts.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="188dp"
android:layout_marginRight="67dp"
android:onClick="speakText"
android:text="@string/text1"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="81dp"
android:ems="10">
<requestFocus/>
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/write"
android:textAppearance="?android:attr/textAppearanceLarge"/>
答案 0 :(得分:0)
第一个错误是你没有在onCreate
中初始化你的按钮,也没有给它一个OnClickListener。这里的另一个问题是您的onInit
。当你覆盖它时,尝试这样解决:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
button1.setEnabled(true);
speakText();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
并且整个代码看起来像这样:
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button speakButton;
private EditText inputText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputText = (EditText) findViewById(R.id.editText1);
tts = new TextToSpeech(this, this);
speakButton = (Button) findViewById(R.id.button1);
// button on click event
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakButton.setEnabled(true);
speakText();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakText() {
String toSpeak = inputText.getText().toString();
tts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}
答案 1 :(得分:0)
我发现的错误很少: -
您不需要实现onInitListener并覆盖onInit()方法,因为您正在使用它并且已经覆盖了OnCreate()方法。
在OnCreate()中的onInit()之后你需要调用speakText()方法。
speakText(inputText的)