这是一款可以语音播报多功能的app。。。下面说说实现代码吧
先看一下效果图:
首先先弄好界面xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity"
android:orientation="vertical"
android:background="@drawable/bei">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择语言"
android:textColor="#c6b821"
android:textSize="30dp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"></Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入需要朗读的文字"
android:textSize="30dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt"
android:background="@drawable/edt"
/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放"
android:textColor="#fff"
android:textSize="30dp"
android:id="@+id/btn"
android:background="@drawable/loing_btns"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束"
android:textColor="#fff"
android:textSize="30dp"
android:background="@drawable/loing_btns"
android:id="@+id/btn1"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="循环播放"
android:textColor="#fff"
android:textSize="30dp"
android:background="@drawable/loing_btns"
android:id="@+id/btn2"/>
</LinearLayout>
然后Mainactivity.java里面
package com.example.myapplication;
import android.content.DialogInterface;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech = null; //TTS
private Spinner spinner ; //下拉列表框
private EditText editText;
private Button button;
private String[] langs;//下拉列表款里面的选项
private String curLang;
private List<String> list = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
langs = getResources().getStringArray(R.array.languages);
spinner = (Spinner)findViewById(R.id.spinner);
editText = (EditText)findViewById(R.id.edt);
button = (Button)findViewById(R.id.btn);
Button button1 = (Button)findViewById(R.id.btn1);
Button button2 = (Button)findViewById(R.id.btn2);
for (int i = 0;i<langs.length;i++){
list.add(langs[i]);
}
langs = getResources().getStringArray(R.array.languages);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,langs);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { //下拉列表框点击事件
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
curLang = (String)spinner.getAdapter().getItem((int)l);
if(textToSpeech!=null){
textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}
textToSpeech = new TextToSpeech(MainActivity.this,new TTSListener()); //给定一个TTS语音
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSpeech.stop();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for(int i = 0;i<10;i++) {
textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
private int SetLanguage(String lang){
int result = 0;
if (lang.equals("中文")){
result = textToSpeech.setLanguage(Locale.CHINESE);
}
else if (lang.equals("英文")){
result = textToSpeech.setLanguage(Locale.ENGLISH);
}
else if (lang.equals("日语")){
result = textToSpeech.setLanguage(Locale.JAPANESE);
}
else if (lang.equals("韩语")){
result = textToSpeech.setLanguage(Locale.KOREAN);
}
else if (lang.equals("法语")){
result = textToSpeech.setLanguage(Locale.FRENCH);
}
else if (lang.equals("意大利语")){
result = textToSpeech.setLanguage(Locale.ITALIAN);
}
return result;
}
private class TTSListener implements TextToSpeech.OnInitListener{
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS){
int result = SetLanguage(curLang);
if(result==TextToSpeech.LANG_MISSING_DATA||result ==TextToSpeech.LANG_NOT_SUPPORTED) {
textToSpeech.speak("你输入的不是该国语言", TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
}