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

在科大讯飞语音包的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()方法。參数传入你想输出的话,大功告成。。!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中调用语音接口方法,可以通过以下步骤进行: 1. 首先,确保你已经在开放平台注册并获取了AppID和API Key。如果还没有,你可以前往开放平台进行注册和申请。 2. 在Android Studio中创建一个新的Android项目,并在项目的`build.gradle`文件中添加语音SDK的依赖。例如,可以在`dependencies`部分添加以下代码: ```groovy implementation 'com.iflytek:speech-plus:8.6.0' ``` 3. 在你的代码中,首先初始化语音SDK。在你的Application类的`onCreate()`方法中添加以下代码: ```java SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID + "=你的AppID"); ``` 4. 接下来,你可以使用语音SDK提供的各种接口进行语音识别、语音合成等操作。例如,如果你想进行语音识别,可以使用以下代码: ```java // 创建SpeechRecognizer对象 SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(getApplicationContext(), null); // 设置参数 mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); mIat.setParameter(SpeechConstant.ACCENT, "mandarin"); // 开始识别 mIat.startListening(mRecognizerListener); ``` 在上述代码中,`mRecognizerListener`是一个实现了`RecognizerListener`接口的监听器,用于接收识别结果和状态回调。 以上是一个简单的示例,你可以根据自己的需求和语音SDK提供的文档进行详细的配置和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值