我想转换我当前的纬度,经度文成speech.I有code查找当前的纬度,longitude.But我初始化Location类里面我只latLongString.I得到无法得到EX的TextToSpeech方法:您当前的位置是北纬= 1.222,长= 22.335。
下面我code:
公共类SpeakActivity扩展活动实现OnInitListener {
私人TextToSpeech TTS;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_speak);
LocationManager locationManager;
字符串情形= Context.LOCATION_SERVICE;
locationManager =(LocationManager)getSystemService(上下文);
标准CRTA =新标准();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(假);
crta.setBearingRequired(假);
crta.setCostAllowed(真正的);
crta.setPowerRequirement(Criteria.POWER_LOW);
字符串商= locationManager.getBestProvider(CRTA,真正的);
//字符串提供商= LocationManager.GPS_PROVIDER;
位置位置= locationManager.getLastKnownLocation(供应商);
TTS =新TextToSpeech(这一点,这一点);
updateWithNewLocation(位置);
}
公共无效发言(字符串text2say){
tts.speak(text2say,TextToSpeech.QUEUE_FLUSH,NULL);
}
@覆盖
公共无效的OnInit(INT状态){
说(latLongString);
}
私人无效updateWithNewLocation(位置定位){
字符串latLongString;
TextView的myLocation;
myLocation =(TextView中)findViewById(R.id.myLocation);
如果(位置!= NULL){
双纬度= location.getLatitude();
双LON = location.getLongitude();
latLongString =纬度:+纬度+\ nLong:+ LON;
}其他{
latLongString =找不到位置;
}
myLocation.setText(您当前的位置是:\ N+ latLongString);
说话(latLongString);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
。getMenuInflater()膨胀(R.menu.activity_speak,菜单);
返回true;
}
@覆盖
公共无效的onDestroy(){
如果(TTS!= NULL){
tts.stop();
tts.shutdown();
}
super.onDestroy();
}}
解决方案
通过你的纬度长字符串成SPEAKOUT()文本,例如,通过SPK给出研究。而调用这个函数来获得说话。
私人无效SPEAKOUT(){
字符串文本= txtText.getText()的toString()。
tts.speak(文字,TextToSpeech.QUEUE_FLUSH,NULL);
}
I want to convert my current latitude,longitude text into speech.I have code for finding current latitude,longitude.But i initialize the TextToSpeech method inside Location class i got only latLongString.I couldn't get EX:your current location is lat=1.222,long=22.335.
Here my Code:
public class SpeakActivity extends Activity implements OnInitListener{
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
tts = new TextToSpeech(this, this);
updateWithNewLocation(location);
}
public void speak(String text2say){
tts.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onInit(int status) {
say("latLongString");
}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocation;
myLocation= (TextView) findViewById(R.id.myLocation);
if(location!=null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lon;
}else{
latLongString="no location found";
}
myLocation.setText("Your current position is:\n" + latLongString);
speak(latLongString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_speak, menu);
return true;
}
@Override
public void onDestroy() {
if (tts!= null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}}
解决方案
Pass your lat long string into text in speakOut() as in example given by spk. And call this function to get speak.
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}