【从零之三(更)】自定义类中调用讯飞语音包错误解决办法

来源:互联网 发布:java数组去重排序 编辑:程序博客网 时间:2024/05/16 23:54
在科大讯飞语音包的Mscdemo中它的方法都是写在Activity中的,这样其实并不是很好,因为Activity只是负责UI交互的,如果项目很简单自然可以,但是一旦比较复杂肯定要自己定义很多包很多类,但是写在Activity中的方法就不能被自己定义的类调用了,咋办尼,那就把方法写在自己的类里就行了。
准备工作:把Msc.jar包和libmsc.so拷贝到自己工程的libs目录下,这样才能用它的方法和类。libmsc.so一定要用自己Id下载的包,因为这个包和你的那个appid是绑定的,拷贝别人的是不行的,会有用户校验失败的错误,我就困扰了很久很久。。。

以语音合成方法为例,我在自己的应用程序中需要调用它的合成函数,所以在自己的类里调用了它的synthetizeInSilence()方法。如下

/** * 使用SpeechSynthesizer合成语音,不弹出合成Dialog.* @param*/private void synthetizeInSilence() {if (null == mSpeechSynthesizer) {//创建合成对象.mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(this);}//设置合成发音人.String role = mSharedPreferences.getString(getString(R.string.preference_key_tts_role),getString(R.string.preference_default_tts_role));//设置发音人mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, role);//获取语速int speed = mSharedPreferences.getInt(getString(R.string.preference_key_tts_speed),50);//设置语速mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, ""+speed);//获取音量.int volume = mSharedPreferences.getInt(getString(R.string.preference_key_tts_volume),50);//设置音量mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME, ""+volume);//获取语调int pitch = mSharedPreferences.getInt(getString(R.string.preference_key_tts_pitch),50);//设置语调mSpeechSynthesizer.setParameter(SpeechConstant.PITCH, ""+pitch);//获取合成文本.Editable editable = mSourceText.getText();String source = null;if (null != editable) {source = editable.toString();}//进行语音合成.mSpeechSynthesizer.startSpeaking(source, this);showTip(String.format(getString(R.string.tts_toast_format),0 ,0));}

这里会遇到几个问题,一个是SpeechSynthesizer.createSynthesizer(this)方法中的this源程序是指Activity对象,因为这个参数要求是Context,即上下文对象,在Activity里可以写this,在自己类里写this就成指代类对象,自然报错了。解决办法是将自己的类继承Application,可以用getApplicationContext()方法获取Context对象。第二个错误就是mSharedPreferences,这里是定义很多参数,可有可无,不定义就用默认值,想定义就直接调用setParameter就可以了,在这偷个懒就用其默认值了,修改后的类书写如下。

package dmcore.outputs;import android.app.Application;import android.content.Context;import com.iflytek.cloud.speech.SpeechError;import com.iflytek.cloud.speech.SpeechSynthesizer;import com.iflytek.cloud.speech.SynthesizerListener;public class MyOutput extends Application implements SynthesizerListener{//缓存对象.//private SharedPreferences mSharedPreferences;//合成对象.private SpeechSynthesizer mSpeechSynthesizer;private static Context context; public void onCreate() {super.onCreate();MyOutput.context = getApplicationContext();}public static Context getAppContext() {return MyOutput.context;}//-------------------------------------------------------------------------// Constructor//-------------------------------------------------------------------------public MyOutput(){}public void SetParameter(){if (mSpeechSynthesizer == null) {//创建合成对象.mSpeechSynthesizer = SpeechSynthesizer.createSynthesizer(context);}/*//设置合成发音人.String role = mSharedPreferences.getString(getString(R.string.preference_key_tts_role),getString(R.string.preference_default_tts_role));//设置发音人mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, role);//获取语速int speed = mSharedPreferences.getInt(getString(R.string.preference_key_tts_speed),50);//设置语速mSpeechSynthesizer.setParameter(SpeechConstant.SPEED, ""+speed);//获取音量.int volume = mSharedPreferences.getInt(getString(R.string.preference_key_tts_volume),50);//设置音量mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME, ""+volume);//获取语调int pitch = mSharedPreferences.getInt(getString(R.string.preference_key_tts_pitch),50);//设置语调mSpeechSynthesizer.setParameter(SpeechConstant.PITCH, ""+pitch);*/}/** * 使用SpeechSynthesizer合成语音,不弹出合成Dialog. * @param */public void synthetizeInSilence(String SourceText) {//进行语音合成.mSpeechSynthesizer.startSpeaking(SourceText, this);}@Overridepublic void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {// TODO Auto-generated method stub}@Overridepublic void onCompleted(SpeechError arg0) {// TODO Auto-generated method stub}@Overridepublic void onSpeakBegin() {// TODO Auto-generated method stub}@Overridepublic void onSpeakPaused() {// TODO Auto-generated method stub}@Overridepublic void onSpeakProgress(int arg0, int arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onSpeakResumed() {// TODO Auto-generated method stub}}

注意!!!还没完,要到Manifest.xml文件的application标签里加上你的类的位置,我的是android:name="dmcore.outputs.MyOutput",当然还要加上那些uses-permission,如下:
<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.permission.ACCESS_WIFI_STATE" />
<uses-permission
android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission 
android:name="android.permission.READ_CONTACTS"/>

主函数中先创建MyOutput对象,再调用SetParameter方法,再调用synthetizeInSilence()方法,参数传入你想输出的话,大功告成!!!

0 0
原创粉丝点击