TextToSpeech服务未启动

时间:2015-02-05 17:25:39

标签: android text-to-speech

TextTo语音没有用。它显示服务没有启动,我已经安装了谷歌播放的文本到语音。当它附加按钮时工作正常但是当我在onCreate()中移动它时停止工作。它绑定使用TTs引擎成功并显示文本长度

public class MainActivity extends Activity implements SensorEventListener{
    private ImageView image;

    // record the compass picture angle turned
    private float currentDegree = 0f;

    // device sensor manager
    private SensorManager mSensorManager;

    TextView tvHeading;

    Campus_guide_Databasehelper cgh;
    float []array=new float[50];
    Button btnShowLocation;
    Button addLocation;
    Button btnshow;
    Button btnNearest_point;
    EditText lat;
    EditText longi;
    EditText texte;
    TextToSpeech ttsobj;
    float bearing;
    // GPSTracker class
    GPSTracker gps;
    Location location;
    Location curr_loc;
    Location dest_loc;
    double latitude;
    double longitude;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compass);
        cgh=new Campus_guide_Databasehelper(this);

        gps = new GPSTracker(MainActivity.this);
        image = (ImageView) findViewById(R.id.imageViewCompass);

        // TextView that will tell the user what degree is he heading
        tvHeading = (TextView) findViewById(R.id.tvHeading);

        // initialize your android device sensor capabilities
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        ttsobj=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status==TextToSpeech.SUCCESS)
            {
                int result=ttsobj.setLanguage(Locale.US);
                if(result==TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED)
                {
                    Log.e("TTS","This Language is Not supported");
                    Intent installation=new Intent();
                    installation.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installation);
                }
                else{
                    Log.e("TTS","Installation Failed");
                }
            }

        }
       });

       ttsobj.speak("speak some thing",TextToSpeech.QUEUE_FLUSH, null);
        // check if GPS enabled     
       if(gps.canGetLocation()){

        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        latitude=Math.round(latitude*1000000.0)/1000000.0;
        longitude=Math.round(longitude*1000000.0)/1000000.0;
        curr_loc=new Location(" ");
        dest_loc=new Location(" ");
        curr_loc.setLatitude(latitude);
        curr_loc.setLongitude(longitude);
        String data=cgh.Nearest_point(latitude, longitude);
        String[] split=data.split(":");
        for(int i=1;i<split.length;i++)
        {
            String [] fur_split=split[i].split(" ");
            Double llti=Double.parseDouble(fur_split[0]);
            Double llong=Double.parseDouble(fur_split[1]);
            dest_loc.setLatitude(llti);
            dest_loc.setLongitude(llong);
            bearing=curr_loc.bearingTo(dest_loc);
            Toast.makeText(this,split[i-1], Toast.LENGTH_LONG).show();
            Toast.makeText(this,String.valueOf(bearing), Toast.LENGTH_LONG).show();


            ttsobj.speak("text to speech works",TextToSpeech.QUEUE_FLUSH,null);

            i++;
        }
        // \n is for new line


        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
      }else{
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
      }

1 个答案:

答案 0 :(得分:0)

首先,您的else位置错误,应该与第一个if匹配

ttsobj=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() {

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if(status==TextToSpeech.SUCCESS)
        {
            int result=ttsobj.setLanguage(Locale.US);
            if(result==TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED)
            {
                Log.e("TTS","This Language is Not supported");
                Intent installation=new Intent();
                installation.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installation);
            }

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

其次,在调用speak之前,您无法致电onInit