我试图将应用程序代码中的工作TTS服务代码复制到磨损代码中但不起作用。
public class VoiceService extends Service implements
TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
private final String TAG = "VoiceService";
private final int LONG_DURATION = 5000;
private final int SHORT_DURATION = 1200;
private TextToSpeech mTts;
private String messageTitle;
private String messageContext;
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
try{
mTts = new TextToSpeech(this, this);
mTts.setOnUtteranceCompletedListener(this);
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.i(TAG, "onStartCommand");
messageTitle = ...
messageContext = ...
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onInit(int status) {
Log.i(TAG, "onInit & status = " + status);
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.getDefault());
Log.i(TAG, "TextToSpeech.SUCCESS");
if (result != TextToSpeech.LANG_MISSING_DATA
&& result != TextToSpeech.LANG_NOT_SUPPORTED
&& messageTitle.length() > 0
) {
Log.i(TAG, "messageTitle.length() > 0");
...
}
}
}
@Override
public void onUtteranceCompleted(String uttId) {
Log.i(TAG, "onUtteranceCompleted");
messageTitle = "";
messageContext = "";
stopSelf();
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
// Text to Speech engine was attached
if (mTts != null) {
mTts.stop();
mTts.shutdown();
mTts = null;
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
...
}
日志简单如下:
I/VoiceService: onCreate
I/VoiceService: onInit & status = -1
I/VoiceService: onStartCommand
我不明白为什么onInit在onStartCommand之前出现,并且失败了,服务永远停留在那里......
我按照一些线程在mainactivity中运行以下代码:
PackageManager pm = getPackageManager();
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
ResolveInfo resolveInfo = pm.resolveActivity( installIntent, PackageManager.MATCH_DEFAULT_ONLY );
if( resolveInfo == null ) {
// Not able to find he activity which should be started for this intent
Log.i(TAG, "Not able to find the activity which should be started for this intent");
} else {
Log.i(TAG, "try to install...");
startActivity( installIntent );
}
日志如下,我无意修复它:
无法找到应为此意图启动的活动
我正在使用Android Wear模拟器而我在设置中找不到TTS启用按钮 - >辅助功能菜单。但是在菜单下,我找到了TalkBack(实验性的),不知道它是什么。
我花了几天时间。今晚有点累。请给我一些如何使TTS在模拟器中工作的方向。