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

}}
4

3 に答える 3

1

spkの例のように、speakOut()のテキストに緯度の長い文字列を渡します。そして、この関数を呼び出して話します。

private void speakOut() {

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

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
于 2012-08-14T05:24:51.540 に答える
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);
    }
}

そして、必要に応じてこれを変更します。

于 2012-08-14T05:27:47.080 に答える
0

話す代わりに(latLongString); あなたはこのように話すことができますspeak(myLocation.getText); だからそれは話しますあなたの現在の位置は「LatLong」です。

于 2014-04-17T05:12:23.960 に答える