怎么说呢,因为要背单词,同时希望能快速获得今日所背单词表的读音。于是想要一款简单的,输入单词表,然后生成列表,点击单词可以获得读音。
查资料后发现安卓自带 TextToSpeech可以实现这个功能,于是尝试写了一个app。
有谁想试试这个可怕的apk可以下载↓
https://pan.baidu.com/s/1guL7CzzLLXgKrwKSYB2DqA
提取码:0xzp
TextToSpeech的api文档
写起来挺简单的,一试令人崩溃……这合成的机械音,感觉自己受到了欺骗……有这个闲工夫不如去背单词……怒把过程写在这,之后找到正确的语音包之后重新写一个正常的。
最终效果
只要用到一个mainActivity:
package com.example.read;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<String> words_list;
private EditText et_words;
private Button btn_get;
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得单词数据
words_list = new ArrayList<String>();
et_words = (EditText) findViewById(R.id.et_words);
btn_get = (Button) findViewById(R.id.btn_get);
TextToSpeech.OnInitListener ttsLisener = new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
System.out.println("数据丢失或不支持");
}
}
}
};
textToSpeech = new TextToSpeech(this, ttsLisener);
btn_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//[1]获取输入框数据并放入words_list
String s = et_words.getText().toString();
String[] words = s.split(" ");
for(int i = 0; i < words.length; i++) {
words_list.add(words[i]);
}
//[2]清空输入框
et_words.setText("");
//展示数据并实现朗读功能
//[1]获取adapter,放入数据
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, words_list);
ListView lv_words = (ListView) findViewById(R.id.lv_words);
lv_words.setAdapter(arrayAdapter);
//设置监听朗读数据
lv_words.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//获得要朗读的单词
String word = words_list.get(position);
if(textToSpeech != null && !textToSpeech.isSpeaking()) {
//textToSpeech.setPitch(0.5f); //设置声调,1是正常,0.5f男声,くわい
textToSpeech.speak(word, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
}
});
}
@Override
public void onDestroy() {
if (textToSpeech!= null) {
//停止TextToSpeech
textToSpeech.stop();
//释放TextToSpeech占用的资源
textToSpeech.shutdown();
}
super.onDestroy();
}
}
布局文件
<LinearLayout 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="com.example.read.MainActivity"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_words"
android:layout_width="match_parent"
android:layout_height="380dp" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入单词用空格隔开"
android:id="@+id/et_words" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:id="@+id/btn_get"
android:text="OK" />
</LinearLayout>
</LinearLayout>
以上