package com.example.patrice.rerservation01.vocale;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;

import com.example.patrice.rerservation01.R;

public class speak_api19 extends AppCompatActivity
{

    private EditText userInputToSpeech;
    private TextToSpeech convertToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speak02_layout);

        userInputToSpeech = (EditText)findViewById(R.id.speech_input);
        Button textToSpeech = (Button)findViewById(R.id.text_to_speech);

        textToSpeech.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final String convertTextToSpeech = userInputToSpeech.getText().toString();
                if (convertTextToSpeech.equals(""))
                {
                    Toast.makeText(speak_api19.this, "The input field must not be empty", Toast.LENGTH_LONG).show();
                    return;
                }

                convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener()
                {
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void onInit(int status)
                    {
                        if (status != TextToSpeech.ERROR)
                        {
                            convertToSpeech.setLanguage(Locale.US);
                            convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
                        }
                    }
                });
            }
        });
    }

}