java在Android TTS中没有声音错误的上下文?
我有一个类DistanceAnnouncer
,它使用Android文本到语音引擎来宣布两个位置之间的距离。它的构造如下:
public class DistanceAnnouncer {
private TextToSpeech speaker;
public DistanceAnnouncer(Context context) {
speaker = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
...
通过以下方法向用户传达距离:
public void speak(Location targetLocation, Location currentLocation) {
float distanceInMeters = targetLocation.distanceTo(currentLocation);
speaker.speak(String.valueOf(distanceInMeters) + " meters",TextToSpeech.QUEUE_FLUSH,null);
在我的MainActivity
中,我创建了一个类级对象
protected void onCreate(Bundle savedInstanceState) {
distanceAnnouncer = new DistanceAnnouncer(this);
单击对话框按钮时,将在匿名方法中使用,如下所示。这个很好用。但现在我想重构代码以使用更少的内存,并想去掉类级变量。为了测试它,我创建了一个新的测试对象,并将其MainActivity.this
作为上下文(与构造函数中接收this
的类级对象相反)。旧对象仍然有效,新对象则无效。没有抛出异常。代码被执行,但没有声音
代码:
public void NewTargetButtonClicked(View view) {
final EditText entryEditText = new EditText(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(R.string.enter_coordinates_prompt);
alert.setView(entryEditText);
alert.setCancelable(true);
alert.setPositiveButton(R.string.ok_button_text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (entryEditText.getText() == null) return;
Location newLocation = getLocationFromCoords(entryEditText.getText().toString());
//pagaidām protam apstrādāt tikai decimālgrādus
if (!(newLocation == null)) {
mTargetLocation = newLocation;
refreshDistance();
Button targetButton = (Button) MainActivity.this.findViewById(R.id.newTargetButton);
targetButton.setText(R.string.set_new_target);
DistanceAnnouncer newDistanceAnnouncer = new DistanceAnnouncer(MainActivity.this);
//THIS DOESN'T WORK:
newDistanceAnnouncer.speak(mTargetLocation,mCurrentLocation);
//THIS WORKS:
distanceAnnouncer.speak(mTargetLocation,mCurrentLocation);
CheckBox speakCheckbox = (CheckBox) MainActivity.this.findViewById(R.id.speakDistanceCheckbox);
speakCheckbox.setVisibility(View.VISIBLE);
}
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
我怀疑这是语境问题,但在这种情况下,两种语境之间的区别是什么?它们应该是一样的,不是吗
编辑:全距离播音员课程代码:
package com.blueit.audioguide;
import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.location.Location;
import 安卓.speech.tts.TextToSpeech;
import java.util.Locale;
/**
* Created by janeks on 23.04.2016..
*/
public class DistanceAnnouncer {
private TextToSpeech speaker;
public DistanceAnnouncer(Context context) {
speaker = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
speaker.setLanguage(Locale.ENGLISH);
}
}
});
}
public void speak(Location targetLocation, Location currentLocation) {
if (targetLocation == null) return;
if (currentLocation == null) return;
if ((!(targetLocation == null)) && (!(currentLocation == null))) {
float distanceInMeters = targetLocation.distanceTo(currentLocation);
if (distanceInMeters < 1000) {
distanceInMeters = Math.round(distanceInMeters);
speaker.speak(String.valueOf(distanceInMeters) + " meters",TextToSpeech.QUEUE_FLUSH,null);
} else if ((distanceInMeters >= 1000) && (distanceInMeters <= 100000)) {
float distanceInKilometers = distanceInMeters / 1000;
distanceInKilometers = Math.round(distanceInKilometers * 10) / 10;
speaker.speak(String.valueOf(distanceInKilometers) + " kilometers",TextToSpeech.QUEUE_FLUSH,null);
} else if (distanceInMeters > 100000) {
float distanceInKilometers = distanceInMeters / 1000;
distanceInKilometers = Math.round(distanceInKilometers);
speaker.speak(String.valueOf(distanceInKilometers) + " kilometers",TextToSpeech.QUEUE_FLUSH,null);
}
} else {
}
}
}
编辑2:应用程序日志,通过TextToSpeech过滤:
04-23 16:00:03.911 30815-30815/com.blueit.audioguide I/TextToSpeech: Sucessfully bound to com.google.安卓.tts
04-23 16:00:03.988 30815-30815/com.blueit.audioguide I/TextToSpeech: Connected to ComponentInfo{com.google.安卓.tts/com.google.安卓.tts.service.GoogleTTSService}
04-23 16:00:03.995 30815-30932/com.blueit.audioguide I/TextToSpeech: Set up connection to ComponentInfo{com.google.安卓.tts/com.google.安卓.tts.service.GoogleTTSService}
04-23 16:01:35.048 30815-30815/com.blueit.audioguide I/TextToSpeech: Sucessfully bound to com.google.安卓.tts
04-23 16:01:44.346 30815-30815/com.blueit.audioguide W/TextToSpeech: speak failed: not bound to TTS engine
似乎系统将第一个连接到系统TTS服务,但没有连接第二个。但原因是什么?我还尝试删除第一个,只留下第二个(可能每个活动不允许有几个TTS?),但没有成功
# 1 楼答案
只需将一个
DistanceAnnouncer
实例作为MainActivity
的成员变量总是在
AlertDialog
中创建一个新实例的问题是TTS引擎不能立即可用。现在你打电话给。。。然后在正确初始化之前,即在
onInit()
运行之前,尝试通过TTS引擎说话