使用自定义数组适配器进行文本转语音 Android
Text To Speech With Custom Array-Adapter Android
我制作了一个自定义适配器,现在我想在其构造函数中向语音添加一个文本,但我不知道该怎么做。
MainActivity
ArrayList<Word> numbers = new ArrayList<Word>();
//Initializing
numbers.add(new Word("one", "uno", R.drawable._nmbericon));
//this is how i want to pass my Pronunciation as the fourth parameter
// numbers.add(new Word("one", "onu", R.drawable._nmbericon,"unoo"));
WordAdapter adapter = new WordAdapter(this, numbers);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
//->> TEXT TO SPEECH STARTS HERE
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textToSpeech = new TextToSpeech(NumbersActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
result = textToSpeech.setLanguage(Locale.ENGLISH);
String toSpeak = "ONUU";
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}else{
Toast.makeText(getApplicationContext(), "Not Supported in your Device", Toast.LENGTH_SHORT).show();
}
}
});
}
});
//->> TEXT TO SPEECH ENDSHERE
WordAdapter
public class WordAdapter extends ArrayAdapter<Word> {
//Constructor
public WordAdapter(Activity context, ArrayList<Word> word) {
super(context, 0, word);
}
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext())
.inflate(R.layout.item_list, parent, false);
}
Word CurrentWord = getItem(position);
TextView spanish_text_view = (TextView) listItemView
.findViewById(R.id.spanish_text_view);
TextView default_text_view = (TextView) listItemView
.findViewById(R.id.default_text_view);
default_text_view.setText(CurrentWord.getEnglishWords());
ImageView icon = (ImageView) listItemView.findViewById(R.id.icon);
icon.setImageResource(CurrentWord.getImageResourceid());
View textContainer = listItemView.findViewById(R.id.textContainer);
return listItemView;
}
}
字
public class Word {
//Varibles
private String SpanishWords; //Spanish Word
private String EnglishWords; //English translation of the Spanish word
private int imageResourceid;
private String pronunciation;
public Word(String SpanishWords, String EnglishWords, int imageResourceid, String pronunciation) {
this.SpanishWords= SpanishWords; //Since the name is the same use the this keyword
this.EnglishWords = EnglishWords;
this.imageResourceid = imageResourceid;
this.pronunciation = pronunciation;
}
//getters
public String getPronunciation() {
return pronunciation;
}
}
所以当我点击 TextContainer
时,应该说西班牙语(这是 MainActivity
class 中的第四个参数)。
这就是我目前所拥有的,但我无法让它发挥作用。每当我尝试播放该应用程序时,该应用程序都会崩溃。
我真的不知道如何通过自定义 arrayAdapter
使用 Text to Speech。
如果有人能指出正确的方向,我将不胜感激。
我想让它直接从 MainActivity Initializing 地方的第四个参数
抓取文本
这应该有效。
如果您对语言有疑问,请查看 official docs。
textoSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textoSpeech.setLanguage(Locale.ES);
}
}
});
TextContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = TextContainer.getText().toString();
Toast.makeText(getApplicationContext(),toSpeak,Toast.LENGTH_SHORT).show();
textoSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
希望我有所帮助。
所以我设法修复了它,我所要做的就是编辑 MainActivity
Java 文件
像这样
MainActivity
final ArrayList<Word> numbers = new ArrayList<Word>(); //edited this
numbers.add(new Word("one", "onu", R.drawable._nmbericon,"unoo"));
WordAdapter adapter = new WordAdapter(this, numbers);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
//->> TEXT TO SPEECH STARTS HERE
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textToSpeech = new TextToSpeech(NumbersActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//And edited Here
if (status == TextToSpeech.SUCCESS){
result = textToSpeech.setLanguage(Locale.ENGLISH);
Word num = numbers.get(position);
result = textToSpeech.setLanguage(Locale.ENGLISH);
String toSpeak = num.getPronunciation();
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}else{
Toast.makeText(getApplicationContext(), "Not Supported in your Device", Toast.LENGTH_SHORT).show();
}
}
});
}
});
//->> TEXT TO SPEECH ENDSHERE
我制作了一个自定义适配器,现在我想在其构造函数中向语音添加一个文本,但我不知道该怎么做。
MainActivity
ArrayList<Word> numbers = new ArrayList<Word>();
//Initializing
numbers.add(new Word("one", "uno", R.drawable._nmbericon));
//this is how i want to pass my Pronunciation as the fourth parameter
// numbers.add(new Word("one", "onu", R.drawable._nmbericon,"unoo"));
WordAdapter adapter = new WordAdapter(this, numbers);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
//->> TEXT TO SPEECH STARTS HERE
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textToSpeech = new TextToSpeech(NumbersActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
result = textToSpeech.setLanguage(Locale.ENGLISH);
String toSpeak = "ONUU";
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}else{
Toast.makeText(getApplicationContext(), "Not Supported in your Device", Toast.LENGTH_SHORT).show();
}
}
});
}
});
//->> TEXT TO SPEECH ENDSHERE
WordAdapter
public class WordAdapter extends ArrayAdapter<Word> {
//Constructor
public WordAdapter(Activity context, ArrayList<Word> word) {
super(context, 0, word);
}
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext())
.inflate(R.layout.item_list, parent, false);
}
Word CurrentWord = getItem(position);
TextView spanish_text_view = (TextView) listItemView
.findViewById(R.id.spanish_text_view);
TextView default_text_view = (TextView) listItemView
.findViewById(R.id.default_text_view);
default_text_view.setText(CurrentWord.getEnglishWords());
ImageView icon = (ImageView) listItemView.findViewById(R.id.icon);
icon.setImageResource(CurrentWord.getImageResourceid());
View textContainer = listItemView.findViewById(R.id.textContainer);
return listItemView;
}
}
字
public class Word {
//Varibles
private String SpanishWords; //Spanish Word
private String EnglishWords; //English translation of the Spanish word
private int imageResourceid;
private String pronunciation;
public Word(String SpanishWords, String EnglishWords, int imageResourceid, String pronunciation) {
this.SpanishWords= SpanishWords; //Since the name is the same use the this keyword
this.EnglishWords = EnglishWords;
this.imageResourceid = imageResourceid;
this.pronunciation = pronunciation;
}
//getters
public String getPronunciation() {
return pronunciation;
}
}
所以当我点击 TextContainer
时,应该说西班牙语(这是 MainActivity
class 中的第四个参数)。
这就是我目前所拥有的,但我无法让它发挥作用。每当我尝试播放该应用程序时,该应用程序都会崩溃。
我真的不知道如何通过自定义 arrayAdapter
使用 Text to Speech。
如果有人能指出正确的方向,我将不胜感激。
我想让它直接从 MainActivity Initializing 地方的第四个参数
这应该有效。 如果您对语言有疑问,请查看 official docs。
textoSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textoSpeech.setLanguage(Locale.ES);
}
}
});
TextContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = TextContainer.getText().toString();
Toast.makeText(getApplicationContext(),toSpeak,Toast.LENGTH_SHORT).show();
textoSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
希望我有所帮助。
所以我设法修复了它,我所要做的就是编辑 MainActivity
Java 文件
像这样
MainActivity
final ArrayList<Word> numbers = new ArrayList<Word>(); //edited this
numbers.add(new Word("one", "onu", R.drawable._nmbericon,"unoo"));
WordAdapter adapter = new WordAdapter(this, numbers);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
//->> TEXT TO SPEECH STARTS HERE
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textToSpeech = new TextToSpeech(NumbersActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//And edited Here
if (status == TextToSpeech.SUCCESS){
result = textToSpeech.setLanguage(Locale.ENGLISH);
Word num = numbers.get(position);
result = textToSpeech.setLanguage(Locale.ENGLISH);
String toSpeak = num.getPronunciation();
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}else{
Toast.makeText(getApplicationContext(), "Not Supported in your Device", Toast.LENGTH_SHORT).show();
}
}
});
}
});
//->> TEXT TO SPEECH ENDSHERE