2012-08-14 3 views
0

현재 위도, 경도 텍스트를 음성으로 변환하고 싶습니다. 현재 위도, 경도를 찾기위한 코드가 있습니다. 위치 클래스 내 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

+0

Spk : 나는 당신의 링크 된 예제를 이미 보았다.하지만 나는 얻을 수 없었다. 내 위치 텍스트 위도, 경도로 연설. – Ram

답변

1

는 SPK에 의해 주어진 예에서와 같이() speakOut 텍스트로 위도 긴 문자열을 전달합니다. 그리고이 기능을 호출하여 말하십시오.

private void speakOut() { 

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

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

Shalini : 이미 길을 시도했지만 ... 작동하지 않습니다. 가능한 모든 방법을 시도했지만 출력을 얻을 수 없었습니다. – Ram

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

+0

@ user1597030 필요한 권한을 추가 했습니까? 그럼, 어떻게 나를 위해 잘 작동합니다. [여기에서보십시오] (http://dl.dropbox.com/u/69670844/LocationSample.zip) – Praveenkumar

+0

SPK : 예 인터넷 및 위치 permission.Emulator가 단순히 출력 helloworld를 보여주었습니다. – Ram

0

대신 speak (latLongString); 다음과 같이 할 수 있습니다 speak (myLocation.getText); 그래서 그것은 말할 것입니다 귀하의 현재 위치는 'LatLong'입니다.