MyPlayActivity.java 2.02 KB
package tonio.noa;

import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Toast;

import java.util.Locale;


/**
 * Created by psyk on 09/01/18.
 */

public abstract class MyPlayActivity extends Activity {

    public static final int REQUEST_CODE = 1;
    protected TextToSpeech tts;
    protected String smallCons;

    protected abstract void next();

    protected void enonceConsigne(View view) {
        tts.speak(smallCons, TextToSpeech.QUEUE_FLUSH, null, null);
    }

    protected void bravoPage(View view) {
        startActivityForResult(new Intent(this, BravoActivity.class), REQUEST_CODE);
    }

    protected void lanceConsigne(String cons) {
        Intent i = new Intent(this, ConsigneActivity.class);
        i.putExtra("keyConsigne", cons);
        int requestCode = 0;
        startActivityForResult(i, requestCode);
    }

    protected void setTts() {

        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.FRENCH);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                this.next();
                finish();
            }
        } catch (Exception ex) {
            Toast.makeText(MyPlayActivity.this, ex.toString(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    protected void backHome(View view) {

        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

}