博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【从零之三(更)】自己定义类中调用讯飞语音包错误解决的方法
阅读量:7108 次
发布时间:2019-06-28

本文共 5174 字,大约阅读时间需要 17 分钟。

在科大讯飞语音包的Mscdemo中它的方法都是写在Activity中的,这样事实上并非非常好。由于Activity仅仅是负责UI交互的,假设项目非常easy自然能够,可是一旦比較复杂肯定要自定义非常多包非常多类。可是写在Activity中的方法就不能被自定义的类调用了。咋办尼,那就把方法写在自己的类里即可了。
准备工作:把Msc.jar包和libmsc.so复制到自己project的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);	}		@Override	public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {		// TODO Auto-generated method stub			}	@Override	public void onCompleted(SpeechError arg0) {		// TODO Auto-generated method stub			}	@Override	public void onSpeakBegin() {		// TODO Auto-generated method stub			}	@Override	public void onSpeakPaused() {		// TODO Auto-generated method stub			}	@Override	public void onSpeakProgress(int arg0, int arg1, int arg2) {		// TODO Auto-generated method stub			}	@Override	public 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()方法。參数传入你想输出的话,大功告成。。!

转载地址:http://lolhl.baihongyu.com/

你可能感兴趣的文章
php提示Fatal error: Call to undefined function imagecreate()
查看>>
Qt移动应用开发(六):QML与C++互动
查看>>
svn代码统计工具的金额
查看>>
2015第32周三
查看>>
Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3
查看>>
OCP-1Z0-051-标题决心-文章2称号
查看>>
Codeforces 56D Changing a String 编辑距离 记忆dp
查看>>
Scala 深入浅出实战经典 第62讲:Scala中上下文界定内幕中的隐式参数实战详解...
查看>>
Android应用Design Support Library完全使用实例
查看>>
中通打印助手-实现快递面单快速打印(免费使用)
查看>>
付款页面DEMO
查看>>
Swift - 使用Core Data进行数据持久化存储
查看>>
android L新控件RecyclerView具体解释DeMo
查看>>
[转载]服务器和应用系统迁移方案
查看>>
ActionBarActivity: cannot be resolved to a type
查看>>
类的专有方法(__init__)
查看>>
open()系统调用的实现
查看>>
java历史集合类对比
查看>>
Java实现字符全阵列阵列
查看>>
媒体类型和字符集
查看>>