百度語音生成AndroidSDK線上功能簡單使用
阿新 • • 發佈:2018-12-19
折騰了半小時,百度語音生成SDK的簡單使用,只能使用網路播放,離線出現BUg未修復,
public class SayActivity extends AppCompatActivity { String AppId = "146***70"; String AppKey = "shg****ld"; String AppSecret = "YY*****WfN2xS"; private static final String TAG = "MiniActivity"; private SpeechSynthesizer mSpeechSynthesizer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_say); initPermission();//許可權檢測 mSpeechSynthesizer = SpeechSynthesizer.getInstance(); mSpeechSynthesizer.setContext(this); // this 是Context的之類,如Activity mSpeechSynthesizer.setAppId(AppId/*這裡只是為了讓Demo執行使用的APPID,請替換成自己的id。*/); mSpeechSynthesizer.setApiKey(AppKey, AppSecret/*這裡只是為了讓Demo正常執行使用APIKey,請替換成自己的APIKey*/); mSpeechSynthesizer.auth(TtsMode.ONLINE); // 純線上 mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "4"); // 設定發聲的人聲音,線上生效 mSpeechSynthesizer.initTts(TtsMode.ONLINE); // 初始化離線上混合模式,如果只需要線上合成功能,使用 TtsMode.ONLINE mSpeechSynthesizer.setSpeechSynthesizerListener(new SpeechSynthesizerListener() {....自己補上去...部落格看著太長...省略若干字....}); //listener是SpeechSynthesizerListener 的實現類,需要實現您自己的業務邏輯。SDK合成後會對這個類的方法進行回撥。 LoggerProxy.printable(true); findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0"); // 設定發聲的人聲音,線上生效 TextInputEditText ed = (TextInputEditText) findViewById(R.id.ed); mSpeechSynthesizer.speak(ed.getText().toString()); } }); findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "3"); // 設定發聲的人聲音,線上生效 List<SpeechSynthesizeBag> bags = new ArrayList<SpeechSynthesizeBag>(); bags.add(getSpeechSynthesizeBag("123456", "0")); bags.add(getSpeechSynthesizeBag("你好", "1")); bags.add(getSpeechSynthesizeBag("使用百度語音合成SDK", "2")); bags.add(getSpeechSynthesizeBag("hello", "3")); bags.add(getSpeechSynthesizeBag("這是一個demo工程", "4")); int result = mSpeechSynthesizer.batchSpeak(bags); } }); } private SpeechSynthesizeBag getSpeechSynthesizeBag(String text, String utteranceId) { SpeechSynthesizeBag speechSynthesizeBag = new SpeechSynthesizeBag(); //需要合成的文字text的長度不能超過1024個GBK位元組。 speechSynthesizeBag.setText(text); speechSynthesizeBag.setUtteranceId(utteranceId); return speechSynthesizeBag; } /** * android 6.0 以上需要動態申請許可權 */ private void initPermission() { String permissions[] = { Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_SETTINGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE }; ArrayList<String> toApplyList = new ArrayList<String>(); for (String perm : permissions) { if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) { toApplyList.add(perm); //進入到這裡代表沒有許可權. } } String tmpList[] = new String[toApplyList.size()]; if (!toApplyList.isEmpty()) { ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { // 此處為android 6.0以上動態授權的回撥,使用者自行實現。 } @Override protected void onDestroy() { super.onDestroy(); mSpeechSynthesizer.release(); } }
需要許可權:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!--以上語音許可權-->
關於匯入官方有介紹,下載後放入jniLIb中跟Assets中 ,直接複製拷貝
app配置中新增一句話自動生成jnilib目錄:
sourceSets.main.jniLibs.srcDirs = ['libs']