我正在使用以下代码。它的工作正常,但我有一个文本列表要转换为语音的问题除外。但它只是将最后一行转换为语音。这是我在代码中将数据放入listview并尝试将其转换为语音的代码:
public class TextSpeech extends ListActivity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private TextView txtText;
private List<Message> mess;
List<String> titless;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speach);
tts = new TextToSpeech(this, this);
BaseFeedParser parser = new BaseFeedParser();
mess = parser.parse();
titless = new ArrayList<String>(mess.size());
speakOut();
}
@SuppressLint("NewApi")
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@SuppressLint("NewApi")
@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 {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut() {
for (Message msg : mess){
titless.add(msg.getTitle());
tts.speak(msg.getTitle(), TextToSpeech.QUEUE_FLUSH, null);
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titless);
this.setListAdapter(adapter);
}
}
最佳答案
您使用过TextToSpeech.QUEUE_FLUSH只是将其更改为TextToSpeech.QUEUE_ADD。根据您的要求,您希望TTS逐一读取。