实现类
import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import android.widget.Button; import com.baidu.tts.auth.AuthInfo; import com.baidu.tts.client.SpeechError; import com.baidu.tts.client.SpeechSynthesizer; import com.baidu.tts.client.SpeechSynthesizerListener; import com.baidu.tts.client.TtsMode; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * Created by tiger on 17-4-6. */ public class BaiDuActivity extends Activity implements View.OnClickListener, SpeechSynthesizerListener { private static final String TAG = "Meta:BaiDuActivity"; private SpeechSynthesizer speechSynthesizer; private String mSampleDirPath; private static final String SAMPLE_DIR_NAME = "baiduTTS"; private static final String SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female.dat"; private static final String SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male.dat"; private static final String TEXT_MODEL_NAME = "bd_etts_text.dat"; private static final String LICENSE_FILE_NAME = "temp_license"; private static final String ENGLISH_SPEECH_FEMALE_MODEL_NAME = "bd_etts_speech_female_en.dat"; private static final String ENGLISH_SPEECH_MALE_MODEL_NAME = "bd_etts_speech_male_en.dat"; private static final String ENGLISH_TEXT_MODEL_NAME = "bd_etts_text_en.dat"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialEnv(); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); initTTs(); } private void initTTs() { speechSynthesizer = SpeechSynthesizer.getInstance(); speechSynthesizer.setContext(this); speechSynthesizer.setSpeechSynthesizerListener(this); speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_TEXT_MODEL_FILE, mSampleDirPath + "/"+TEXT_MODEL_NAME); speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TTS_SPEECH_MODEL_FILE, mSampleDirPath + "/"+SPEECH_FEMALE_MODEL_NAME); speechSynthesizer.setAppId(AppId); speechSynthesizer.setApiKey(AppKey, SecretKey); speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0"); speechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_DEFAULT); AuthInfo authInfo = speechSynthesizer.auth(TtsMode.MIX); if (authInfo.isSuccess()) { Log.d(TAG,"auth success"); } else { String errorMsg = authInfo.getTtsError().getDetailMessage(); Log.d(TAG,"auth failed errorMsg=" + errorMsg); } speechSynthesizer.initTts(TtsMode.MIX); // speechSynthesizer.loadModel(mSampleDirPath + "/" + TEXT_MODEL_NAME, mSampleDirPath // + "/" + SPEECH_FEMALE_MODEL_NAME); } @Override public void onClick(View view) { Log.d(TAG,"begin to speak"); speechSynthesizer.speak("百度一下"); } @Override public void onSynthesizeStart(String s) { Log.d(TAG,"onSynthesizeStart s="+s); } @Override public void onSynthesizeDataArrived(String s, byte[] bytes, int i) { Log.d(TAG,"onSynthesizeDataArrived s="+s+", i="+i); } @Override public void onSynthesizeFinish(String s) { Log.d(TAG,"onSynthesizeFinish s="+s); } @Override public void onSpeechStart(String s) { Log.d(TAG,"onSpeechStart s="+s); } @Override public void onSpeechProgressChanged(String s, int i) { Log.d(TAG,"onSpeechProgressChanged s="+s+", i = "+i); } @Override public void onSpeechFinish(String s) { Log.d(TAG,"onSpeechFinish s="+s); } @Override public void onError(String s, SpeechError speechError) { Log.d(TAG,"onError error = "+speechError.toString()); } private void initialEnv() { if (mSampleDirPath == null) { String sdcardPath = Environment.getExternalStorageDirectory().toString(); mSampleDirPath = sdcardPath + "/" + SAMPLE_DIR_NAME; } makeDir(mSampleDirPath); copyFromAssetsToSdcard(false, SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_FEMALE_MODEL_NAME); copyFromAssetsToSdcard(false, SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + SPEECH_MALE_MODEL_NAME); copyFromAssetsToSdcard(false, TEXT_MODEL_NAME, mSampleDirPath + "/" + TEXT_MODEL_NAME); // copyFromAssetsToSdcard(false, LICENSE_FILE_NAME, mSampleDirPath + "/" + LICENSE_FILE_NAME); copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_SPEECH_FEMALE_MODEL_NAME); copyFromAssetsToSdcard(false, "english/" + ENGLISH_SPEECH_MALE_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_SPEECH_MALE_MODEL_NAME); copyFromAssetsToSdcard(false, "english/" + ENGLISH_TEXT_MODEL_NAME, mSampleDirPath + "/" + ENGLISH_TEXT_MODEL_NAME); } private void makeDir(String dirPath) { File file = new File(dirPath); if (!file.exists()) { file.mkdirs(); } } /** * 将sample工程需要的资源文件拷贝到SD卡中使用(授权文件为临时授权文件,请注册正式授权) * * @param isCover 是否覆盖已存在的目标文件 * @param source * @param dest */ private void copyFromAssetsToSdcard(boolean isCover, String source, String dest) { File file = new File(dest); if (isCover || (!isCover && !file.exists())) { InputStream is = null; FileOutputStream fos = null; try { is = getResources().getAssets().open(source); String path = dest; fos = new FileOutputStream(path); byte[] buffer = new byte[1024]; int size = 0; while ((size = is.read(buffer, 0, 1024)) >= 0) { fos.write(buffer, 0, size); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } } }
不要忘记.so导入.dat导入