Browse Source

生成 wav

liuyuqi-dellpc 5 years ago
parent
commit
cc906c32e0

+ 2 - 4
README.md

@@ -1,5 +1,3 @@
 借助科大讯飞sdk 的文字转语音小应用
 借助科大讯飞sdk 的文字转语音小应用
-<p/>
-APK下载链接: <a>https://github.com/ZhangQixiangChina/XunfeiVoice/raw/master/XunfeiVoice.apk<a/>
-<p/>
-<img src="pics/screenshort.png" width = "300" height = "534"/>
+
+

+ 1 - 1
app/build.gradle

@@ -26,7 +26,7 @@ dependencies {
     })
     })
     implementation 'com.android.support:appcompat-v7:28.0.0'
     implementation 'com.android.support:appcompat-v7:28.0.0'
     implementation 'com.android.support:cardview-v7:28.0.0'
     implementation 'com.android.support:cardview-v7:28.0.0'
-    annotationProcessor 'com.jakewharton:butterknife:8.4.0'
+    implementation 'com.jakewharton:butterknife:8.4.0'
     annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
     annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
     compile files('libs/Msc.jar')
     compile files('libs/Msc.jar')
     implementation 'homhomlin.lib:sldinglayout:0.9.0'
     implementation 'homhomlin.lib:sldinglayout:0.9.0'

+ 64 - 3
app/src/main/java/com/zqx/kedaxunfei/activity/MainActivity.java

@@ -1,7 +1,12 @@
 package com.zqx.kedaxunfei.activity;
 package com.zqx.kedaxunfei.activity;
 
 
+import android.Manifest;
 import android.app.Dialog;
 import android.app.Dialog;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Bundle;
+import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView;
@@ -10,9 +15,12 @@ import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.ListView;
 import android.widget.SeekBar;
 import android.widget.SeekBar;
 import android.widget.TextView;
 import android.widget.TextView;
+import android.widget.Toast;
 
 
 import com.iflytek.cloud.SpeechConstant;
 import com.iflytek.cloud.SpeechConstant;
+import com.iflytek.cloud.SpeechError;
 import com.iflytek.cloud.SpeechSynthesizer;
 import com.iflytek.cloud.SpeechSynthesizer;
+import com.iflytek.cloud.SynthesizerListener;
 import com.zqx.kedaxunfei.R;
 import com.zqx.kedaxunfei.R;
 import com.zqx.kedaxunfei.adapter.SpeakerAdapter;
 import com.zqx.kedaxunfei.adapter.SpeakerAdapter;
 import com.zqx.kedaxunfei.bean.Speaker;
 import com.zqx.kedaxunfei.bean.Speaker;
@@ -21,6 +29,7 @@ import com.zqx.kedaxunfei.utils.SpUtil;
 import com.zqx.kedaxunfei.view.BottomDialog;
 import com.zqx.kedaxunfei.view.BottomDialog;
 
 
 import java.util.List;
 import java.util.List;
+import java.util.Random;
 
 
 import butterknife.BindView;
 import butterknife.BindView;
 import butterknife.ButterKnife;
 import butterknife.ButterKnife;
@@ -43,6 +52,9 @@ public class MainActivity extends AppCompatActivity {
     private List<Speaker> mSpeakers ;
     private List<Speaker> mSpeakers ;
     private Speaker mLastSpk;
     private Speaker mLastSpk;
 
 
+    String text;
+    Context mContext;
+
     @Override
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
@@ -52,7 +64,7 @@ public class MainActivity extends AppCompatActivity {
         mSpeakers = Speaker.getAllSpeakers();//得到发音人模型的集合
         mSpeakers = Speaker.getAllSpeakers();//得到发音人模型的集合
         initLast();//初始化上一次退出时的EditText内容和上次选择的发音人数据
         initLast();//初始化上一次退出时的EditText内容和上次选择的发音人数据
         initDialogLv();//初始化发音人选择对话框
         initDialogLv();//初始化发音人选择对话框
-
+        mContext=this;
     }
     }
 
 
     private void initLast() {
     private void initLast() {
@@ -73,6 +85,12 @@ public class MainActivity extends AppCompatActivity {
         mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
         mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
         mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围0~100
         mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围0~100
         mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
         mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
+                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
+            }
+        }
     }
     }
 
 
     private void setSpeed(int num) {
     private void setSpeed(int num) {
@@ -107,10 +125,53 @@ public class MainActivity extends AppCompatActivity {
     }
     }
 
 
     public void onSpeakClick(View view) {
     public void onSpeakClick(View view) {
-        String text = mEt.getText().toString().trim();
+        text = mEt.getText().toString().trim();
         int progress = mSeekBar.getProgress();
         int progress = mSeekBar.getProgress();
         setSpeed(progress);
         setSpeed(progress);
-        mTts.startSpeaking(text, null);
+        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
+        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,Environment.getExternalStorageDirectory()+"/tts/"+text.substring(0, 5)+ new Random().nextInt(100) +".wav");
+        mTts.startSpeaking(text, new MySynthesizerListener());
+    }
+    class MySynthesizerListener implements SynthesizerListener {
+
+        @Override
+        public void onSpeakBegin() {
+
+        }
+
+        @Override
+        public void onBufferProgress(int percent, int beginPos, int endPos ,
+                                     String info) {
+            // 合成进度 完成发送数据到 pc
+            if(percent==100){
+                Toast.makeText(mContext, "合成完成!", Toast.LENGTH_LONG).show();
+            }
+        }
+
+        @Override
+        public void onSpeakPaused() {
+
+        }
+
+        @Override
+        public void onSpeakResumed() {
+
+        }
+
+        @Override
+        public void onSpeakProgress(int i, int i1, int i2) {
+
+        }
+
+        @Override
+        public void onCompleted(SpeechError speechError) {
+
+        }
+
+        @Override
+        public void onEvent(int i, int i1, int i2, Bundle bundle) {
+
+        }
     }
     }
 
 
     public void onSpkerSwitchClick(View view) {
     public void onSpkerSwitchClick(View view) {

+ 2 - 2
app/src/main/java/com/zqx/kedaxunfei/bean/Speaker.java

@@ -12,11 +12,11 @@ import java.util.List;
 
 
 public enum Speaker {
 public enum Speaker {
 
 
+    xiaowanzi(R.mipmap.xiaowanzi, "小丸子 卡通人物", "xiaowanzi"),
     xiaoyan(R.mipmap.xiaoyan, "小燕 青年女声", "xiaoyan"),
     xiaoyan(R.mipmap.xiaoyan, "小燕 青年女声", "xiaoyan"),
     laosun(R.mipmap.laosun, "老孙 中年男声", "vils"),
     laosun(R.mipmap.laosun, "老孙 中年男声", "vils"),
     tanglaoya(R.mipmap.tanglaoya, "唐老鸭 卡通人物", "aisduck"),
     tanglaoya(R.mipmap.tanglaoya, "唐老鸭 卡通人物", "aisduck"),
     xiaoxin(R.mipmap.xiaoxin, "小新 卡通人物", "xiaoxin"),
     xiaoxin(R.mipmap.xiaoxin, "小新 卡通人物", "xiaoxin"),
-    xiaowanzi(R.mipmap.xiaowanzi, "小丸子 卡通人物", "xiaowanzi"),
     xiaomei(R.mipmap.xiaomei, "粤语 小梅 青年女声", "xiaomei"),
     xiaomei(R.mipmap.xiaomei, "粤语 小梅 青年女声", "xiaomei"),
     dengziqi(R.mipmap.dengziqi, "台普 小玲 青年女声", "aisxlin"),
     dengziqi(R.mipmap.dengziqi, "台普 小玲 青年女声", "aisxlin"),
     xiaoqian(R.mipmap.xiaoqian, "东北 小倩 青年女声", "xiaoqian"),
     xiaoqian(R.mipmap.xiaoqian, "东北 小倩 青年女声", "xiaoqian"),
@@ -39,11 +39,11 @@ public enum Speaker {
     public static List<Speaker> getAllSpeakers() {
     public static List<Speaker> getAllSpeakers() {
         return new ArrayList<>(
         return new ArrayList<>(
                 Arrays.asList(
                 Arrays.asList(
+                        xiaowanzi,
                         xiaoyan,
                         xiaoyan,
                         laosun,
                         laosun,
                         tanglaoya,
                         tanglaoya,
                         xiaoxin,
                         xiaoxin,
-                        xiaowanzi,
                         xiaomei,
                         xiaomei,
                         dengziqi,
                         dengziqi,
                         xiaoqian,
                         xiaoqian,

+ 2 - 1
app/src/main/res/layout/activity_main.xml

@@ -27,9 +27,10 @@
             android:background="@drawable/shape_bg"
             android:background="@drawable/shape_bg"
             android:gravity="top"
             android:gravity="top"
             android:hint="@string/please_input"
             android:hint="@string/please_input"
-            android:minLines="8"
+            android:lines="8"
             android:autofillHints=""
             android:autofillHints=""
             android:padding="5dp"
             android:padding="5dp"
+            android:inputType="textMultiLine"
             android:textSize="20sp" />
             android:textSize="20sp" />
 
 
         <android.support.v7.widget.CardView
         <android.support.v7.widget.CardView