2012-08-14 25 views
0

我想将当前的纬度,经度文本转换为语音。我有用于查找当前纬度,经度的代码。但是我初始化Location类中的TextToSpeech方法,只获取latLongString。我无法得到EX:您当前的位置是lat = 1.222,long = 22.335。如何将当前的纬度,经度文本转换为语音

这里我的代码:

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(); 

}} 
+0

那么,你是什么? [看看这个例子](http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/) – Praveenkumar 2012-08-14 04:47:52

+0

Spk:我已经看到了你的链接例子,但我无法得到我的位置文本的纬度,经度转化为语音。 – Ram 2012-08-14 05:01:25

回答

1

通过你的经纬度长字符串转换成文本SPEAKOUT()作为例如通过SPK给出英寸并调用这个函数来获得发言权。

private void speakOut() { 

     String text = txtText.getText().toString(); 

     tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
    } 
+0

Shalini:我已经试过你的方式......但它不工作。我尝试了所有可能的方法,但我无法得到输出。 – Ram 2012-08-14 06:06:58

1

按照以下所示更改onInit。你只会犯那个错误。

@Override 
public void onInit(int status) { 
    // TODO Auto-generated method stub 

    if (status == TextToSpeech.SUCCESS) { 

     int result = tts.setLanguage(Locale.ENGLISH); 

     // tts.setPitch(5); // set pitch level 

     // tts.setSpeechRate(0); // set speech speed rate 

     if (result == TextToSpeech.LANG_MISSING_DATA 
       || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      Log.e("TTS", "Language is not supported"); 
     } else { 
     } 

    } else { 
     Log.e("TTS", "Initilization Failed"); 
    } 

} 

否则,请尝试这个工作示例。

public class LocationSampleActivity extends Activity implements OnInitListener 
{ 
    TextView tv; 
    private TextToSpeech tts; 
    Location speakLoc; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);   
     tv = (TextView)this.findViewById(R.id.txtLocation); 
     tts = new TextToSpeech(this, this); 
     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener ll = new mylocationlistener(); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);  
    } 

    private class mylocationlistener implements LocationListener 
    { 
     @Override 
     public void onLocationChanged(Location location) {  
      if (location != null) { 
      Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
      Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
      String str = "\n CurrentLocation: "+ 
       "\n Latitude: "+ location.getLatitude() + 
       "\n Longitude: " + location.getLongitude();  
       tv.append(str); 
       speak(location); 
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Toast.makeText(LocationSampleActivity.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show(); 
     }  
     @Override 
     public void onProviderEnabled(String provider) { 
      Toast.makeText(LocationSampleActivity.this,"onProviderEnabled",Toast.LENGTH_LONG).show(); 
     } 
     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(LocationSampleActivity.this,"onStatusChanged",Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onInit(int status) { 
     // TODO Auto-generated method stub 

     if (status == TextToSpeech.SUCCESS) { 

      int result = tts.setLanguage(Locale.ENGLISH); 

      // tts.setPitch(5); // set pitch level 

//   tts.setSpeechRate(0); // set speech speed rate 

      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "Language is not supported"); 
      } else { 
      } 

     } else { 
      Log.e("TTS", "Initilization Failed"); 
     } 

    } 

    public void speak(Location lo) 
    { 
     speakLoc = lo; 

     double lat = speakLoc.getLatitude(); 
     double lon = speakLoc.getLongitude(); 

     String speak = "Your location is:" + lat + lon; 

     tts.speak(speak, TextToSpeech.QUEUE_FLUSH, null); 
    } 
} 

并且,根据您的需要修改此内容。

+0

SPK:我没有从你的代码中得到任何输出...... ... – Ram 2012-08-14 06:05:46

+0

@ user1597030您是否添加了所需的权限。那么,它对我的​​工作很好。 [看看这里](http://dl.dropbox.com/u/69670844/LocationSample.zip) – Praveenkumar 2012-08-14 06:07:05

+0

SPK:是的,我添加了互联网和位置权限。模拟器只是显示输出helloworld。 – Ram 2012-08-14 06:15:07

0

而不是 speak(latLongString); 你可以这样做 speak(myLocation.getText);所以它会说你现在的位置是'LatLong'。