Integrated Android Free speech synthesis function (online, offline, off-line fusion), this article is Enough (offline)
Integrated Android free speech synthesis (online, offline, off-line fusion), this article is enough (from the online fusion)
Blink of an eye, the big six months did not write the article, there is no reason, is people become lazy. Embarrassed ~
Look at the title, in fact, we have been cheated, there is not enough of this article, I actually intend to divide 3 articles to write, if it is too long in a chapter, but now the title of the party Rampant network world, I have been contaminated, ha.
So why do you want to divide 3 articles? See the title can also be guessed, is online, offline, from the online fusion of 3 kinds of speech synthesis, I will use the Iflytek, cloud-aware, Baidu Voice to achieve the audio synthesis of Android. As to what are the advantages and disadvantages, we can Baidu, here will no longer talk about, OK, do not say nonsense, I am afraid that someone will hit me, haha ~
First of all, this article first write with the Iflytek to achieve the online speech synthesis Bar, is free of charge, its offline is to money, there is a need to their own official website to see.
Open the Flying open platform
Register, login to the developer account
Go to the console and create a new app
Open the online speech synthesis service
Download SDK
After downloading the SDK, unzip the Tarball Sample-->speechdemo
Open the Speechdemo-->libs folder, copy the jar package and the. So file into your project, and then sync
Once you've done the preparation, you're ready to start your code.
First of all, the Androidmanifest.xml application Authority (6.0 need to dynamically apply for permission, due to the length, please own Baidu)
[HTML] View plain copy
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android. www.chuangyed.com permission. Access_wifi_state "/>
<uses-permission android:name= "Android Www.wmyl15.com/permission. Change_network_state "/>
<uses-permission android:name= "Android. Www.senta77.com/permission. Read_phone_state "/>
<uses-permission android:name= "Android. Www.xucaizxyl.com/permission. Access_fine_location "/>
<uses-permission android:name= "Android. Www.lieqibiji.com/permission. Read_contacts "/>
<uses-permission android:name= "Android. www.dejiaylsmile.cn permission. Write_external_storage "/>
<uses-permission android:name= "Android. Www.6788878.cn/permission. Write_settings "/>
<uses-permission android:name= "Android. www.caihonyule.com permission. Read_external_storage "/>
Then, create a new Myapplication.java file, initialize the speech synthesis Sdk,appid can be viewed on the flight platform--my app, or view the Downloaded SDK Compression pack name suffix
[Java] View plain copy
Package Com.cyf.ttsdemo;
Import android.app.Application;
Import com.iflytek.cloud.SpeechUtility;
/**
* Created by as on 2017/8/7.
*/
public class MyApplication extends application{
@Override
public void OnCreate () {
Super.oncreate ();
Speechutility.createutility (This, "appid=5987d170");
}
}
Finally remember to register the application in Androidmanifest.xml
In order to be able to call the speech synthesis function globally, I encapsulated a tool class, you can directly copy the past
[Java] View plain copy
Package com.cyf.ttsdemo.utils;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.util.Log;
Import com.cyf.ttsdemo.MyApplication;
Import Com.iflytek.cloud.ErrorCode;
Import Com.iflytek.cloud.InitListener;
Import com.iflytek.cloud.SpeechConstant;
Import Com.iflytek.cloud.SpeechError;
Import com.iflytek.cloud.SpeechEvent;
Import Com.iflytek.cloud.SpeechSynthesizer;
Import Com.iflytek.cloud.SynthesizerListener;
/**
* Created by as on 2017/8/7.
*/
public class Ttsutils implements Initlistener, Synthesizerlistener {
private static final String TAG = "Ttsutils";
Private static volatile ttsutils instance = null;
Private Boolean isinitsuccess = false;
Private SpeechSynthesizer mTts;
Private Ttsutils () {
}
public static Ttsutils getinstance () {
if (instance = = null) {
Synchronized (Ttsutils.class) {
if (instance = = null) {
Instance = new Ttsutils ();
}
}
}
return instance;
}
public void init () {
Initializing a Composition Object
MTts = Speechsynthesizer.createsynthesizer (Www.qinlinyule.cn/MyApplication.getContext (), this);
Empty parameters
Mtts.setparameter (speechconstant.params, NULL);
Set up an online compositing engine
Mtts.setparameter (Speechconstant.engine_type, Speechconstant.type_cloud);
Set up online synth pronunciation people
Mtts.setparameter (Speechconstant.voice_name, "XiaoYan");
Set the composition speed
Mtts.setparameter (Speechconstant.speed, "50");
Set composition tones
Mtts.setparameter (Speechconstant.pitch, "50");
Set Composition Volume
Mtts.setparameter (Speechconstant.volume, "50");
Set the player audio stream type
Mtts.setparameter (Speechconstant.stream_type, "3");
Set Play composition audio interrupt music playback, default to True
Mtts.setparameter (Speechconstant.key_request_focus, "true");
Set audio save path, save audio format support PCM, WAV, set path to SD card please note Write_external_storage permissions
Note: Audio_format parameter requires an updated version to take effect
Mtts.setparameter (Speechconstant.audio_format, "wav");
Mtts.setparameter (Speechconstant.tts_audio_path, environment.getexternalstoragedirectory () + "/msc/tts.wav");
}
public void Speak (String msg) {
if (isinitsuccess) {
if (mtts.isspeaking ()) {
Stop ();
}
Mtts.startspeaking (msg, this);
}else {
Init ();
}
}
public void Pause () {
Mtts.pausespeaking ();
}
public void Resume () {
Mtts.resumespeaking ();
}
public void Stop () {
Mtts.stopspeaking ();
}
public void release () {
if (null! = MTts) {
Mtts.stopspeaking ();
Release connection when exiting
Mtts.destroy ();
}
}
@Override
public void OnInit (int code) {
LOG.D (TAG, "Initlistener init () code =" + code);
if (code = = errorcode.success) {
Isinitsuccess = true;
}
}
@Override
public void Onspeakbegin () {
Start playback
}
@Override
public void onbufferprogress (int percent, int beginpos, int endpos,
String info) {
Composition Progress
}
@Override
public void onspeakpaused () {
Pause Playback
}
@Override
public void onspeakresumed () {
Continue playing
}
@Override
public void onspeakprogress (int percent, int beginpos, int endpos) {
Play Progress
}
@Override
public void oncompleted (Speecherror speecherror) {
if (speecherror! = null) {
LOG.D (TAG, "oncompleted:" + speecherror.getplaindescription (true));
}
}
@Override
public void onEvent (int eventtype, int i1, int i2, bundle bundle) {
The following code is used to get the session ID with the cloud, to provide the session ID to the support staff when the business is wrong, to query the session log, to locate the cause of the error
if (speechevent.event_session_id = = EventType) {
String sid = Bundle.getstring (speechevent.key_event_session_id);
LOG.D (TAG, "Session id =" + SID);
}
}
}<strong>
</strong>
To use this tool class, you need to retrofit the previous Myapplication.java file.
[Java] View plain copy
Package Com.cyf.ttsdemo;
Import android.app.Application;
Import Android.content.Context;
Import Com.cyf.ttsdemo.utils.TTSUtils;
Import com.iflytek.cloud.Setting;
Import com.iflytek.cloud.SpeechUtility;
/**
* Created by as on 2017/8/7.
*/
public class MyApplication extends application {
private static context context;
@Override
public void OnCreate () {
Super.oncreate ();
context = Getapplicationcontext ();
Speechutility.createutility (This, "appid=5987d170");
The following statement is used to set the log switch (default on), set to False when you turn off Voice Cloud SDK log Printing
Setting.setshowlog (FALSE);
Ttsutils.getinstance (). Init ();
}
public static Context GetContext () {
return context;
}
}
OK, so it's done, call Ttsutils.getinstance () at the place where you need to make the speech synthesis. Speak ("xxx")
Finally, we need to go to the flying open platform for upload application audit, or the use of speech synthesis function is limited to a number of times per day.
When the app is approved, you can use the online speech synthesis feature for free and unlimited.
Integrated Android free speech synthesis (online, offline, off-line fusion)