DistanceAnnouncer
Android Text-to-speech エンジンを使用して 2 つの場所間の距離をアナウンスするクラスがあります。次のように構成されています。
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
Context として指定しました (コンストラクターで を受け取るクラスレベルのオブジェクトとは対照的に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();
}
これはコンテキストの問題だと思いますが、この場合、2 つのコンテキストの違いは何ですか? それらは同じであるべきですよね?
編集: Full DistanceAnnouncer クラス コード:
package com.blueit.audioguide;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.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 {
}
}
}
EDIT 2: TextToSpeechでフィルタリングされたアプリケーション ログ:
04-23 16:00:03.911 30815-30815/com.blueit.audioguide I/TextToSpeech: Sucessfully bound to com.google.android.tts
04-23 16:00:03.988 30815-30815/com.blueit.audioguide I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
04-23 16:00:03.995 30815-30932/com.blueit.audioguide I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
04-23 16:01:35.048 30815-30815/com.blueit.audioguide I/TextToSpeech: Sucessfully bound to com.google.android.tts
04-23 16:01:44.346 30815-30815/com.blueit.audioguide W/TextToSpeech: speak failed: not bound to TTS engine
システムは最初のものをシステム TTS サービスに接続しますが、2 番目のものには接続しないようです。しかし、どのような理由で?また、最初の 1 つを削除して 2 番目だけを残そうとしましたが (アクティビティごとに複数の TTS が許可されていない可能性がありますか?)、成功しませんでした。