0

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

}}
Ram
  • 1,687
  • 3
  • 18
  • 28
  • Then, what'd you? [Have a look at this example](http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/) – Praveenkumar Aug 14 '12 at 04:47
  • Spk: I already saw your linked example.But i couldn't get my location text latitude,longitude into speech. – Ram Aug 14 '12 at 05:01

3 Answers3

1

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);
    }
Shalini
  • 1,733
  • 4
  • 17
  • 31
  • Shalini: I already tried your way...But it is not working.I tried all possible ways but i couldn't get output.. – Ram Aug 14 '12 at 06:06
1

Change your onInit as per below. You'd mistake with that one only.

@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");
    }

}

Otherwise, try this working example.

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

And, modify this with your needs.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • @user1597030 Have you added required permission. Then, how its working fine for me. [Have a look at here](http://dl.dropbox.com/u/69670844/LocationSample.zip) – Praveenkumar Aug 14 '12 at 06:07
  • SPK:yes i added internet and location permission.Emulator simply showed output helloworld. – Ram Aug 14 '12 at 06:15
  • SPK: Your code is good. I understood.This is the method for my expectation.But what happened why my code was not working.... – Ram Aug 14 '12 at 06:21
  • SPK: Error on provider disabled i got. I think i want to change NETWORK_PROVIDER into GPS_PROVIDER? – Ram Aug 14 '12 at 06:26
0

instead of speak(latLongString); you can do like this speak(myLocation.getText); so it will speak Your current position is 'LatLong'.