仅仅调用安卓手机自带的语音工具,不需要调用诸如讯飞、百度等第三方接口
下面提供一个demo,有需要的同学就可以自行修改复用。
1. 新建一个安卓项目,布局,添加输入框(用来输入要播放的数字),添加一个按钮(触发事件)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<Button
android:id="@+id/btn_video"
android:layout_width="174dp"
android:layout_height="114dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.513" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintBottom_toTopOf="@+id/btn_video"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.542"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 主体实现类(主要用了自带的SoundPool类)
package com.example.videodemo;
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button button1;
private EditText mEt;
private SoundPool mSoundPool = null;
private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
initSP();
} catch (Exception e) {
e.printStackTrace();
}
bindViews();
}
private void initSP() {
//设置最多可以容纳的音频流
mSoundPool = new SoundPool(11,AudioManager.STREAM_SYSTEM, 2);
soundID.put(1, mSoundPool.load(this, R.raw.sound_1, 1));
soundID.put(2, mSoundPool.load(this, R.raw.sound_2, 1));
soundID.put(3, mSoundPool.load(this, R.raw.sound_3, 1));
soundID.put(4, mSoundPool.load(this, R.raw.sound_4, 1));
soundID.put(5, mSoundPool.load(this, R.raw.sound_5, 1));
soundID.put(6, mSoundPool.load(this, R.raw.sound_6, 1));
soundID.put(7, mSoundPool.load(this, R.raw.sound_7, 1));
soundID.put(8, mSoundPool.load(this, R.raw.sound_8, 1));
soundID.put(9, mSoundPool.load(this, R.raw.sound_9, 1));
soundID.put(0, mSoundPool.load(this, R.raw.sound_0, 1));
soundID.put(11, mSoundPool.load(this, R.raw.sound_bobao, 1));
}
//根据 输入的数字 选择需要播放的语音
public void soundPick(){
mEt = (EditText) findViewById(R.id.editText);
String s = mEt.getText().toString();
String[] str = new String[s.length()];
for (int i = 0; i < s.length(); i++){
str[i] = s.substring(i, i+1);
try {
Thread.sleep(600);
mSoundPool.play(soundID.get(Integer.parseInt(str[i])),1,1,0,0,1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//按钮监听
private void bindViews() {
button1 = (Button) findViewById(R.id.btn_video);
button1.setOnClickListener(MainActivity.this);
}
public void onClick(View v) {
soundPick();
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
mSoundPool.play(soundID.get(11),1,1,0,0,1);
}
}
PS:
这个实现方法需要在资源文件中导入相关的音频文件,从0~9十个数字的独立音频(资源我会放到我的主页里面,有需要的可以自己去download)。
没有用到外部的语音接口(商用的话,外部语音接口要钱……)。
主要思想就是拼接,把输入的一串字符拆成一个一个的数字,然后拿每一个数字去匹配对应的音频文件,播放。其中用到了线程休眠,免得一段还没说完,另一段就抢着说的情况。