【问题标题】:How to speak current latitude,longitude text into speech如何将当前纬度、经度文本朗读成语音
【发布时间】:2012-08-14 04:35:24
【问题描述】:

我想将我当前的纬度、经度文本转换为语音。我有用于查找当前纬度、经度的代码。但是我在 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();

}}

【问题讨论】:

  • 那么,你呢? Have a look at this example
  • Spk:我已经看到了您的链接示例。但我无法将我的位置文本纬度、经度转换为语音。

标签: android google-maps gps text-to-speech


【解决方案1】:

将您的 lat long 字符串传递到 speakOut() 中的文本中,如 spk 给出的示例。并调用此函数进行发言。

private void speakOut() {

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

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

【讨论】:

  • Shalini:我已经尝试过你的方法......但它不起作用。我尝试了所有可能的方法,但我无法获得输出......
【解决方案2】:

如下更改您的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);
    }
}

并且,根据您的需要进行修改。

【讨论】:

  • @user1597030 您是否添加了所需的权限。然后,它对我来说如何工作得很好。 Have a look at here
  • SPK:是的,我添加了互联网和位置权限。模拟器只是显示了输出 helloworld。
  • SPK:你的代码很好。我明白了。这是我期望的方法。但是为什么我的代码不起作用....
  • SPK:我得到的提供商禁用错误。我想我想将 NETWORK_PROVIDER 更改为 GPS_PROVIDER?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多