package cisc181.simplemultimedia;

/*

SimpleMultimedia app -- background music and text to speech

Christopher Rasmussen
copyright 2017, University of Delaware

*/

    import android.media.MediaPlayer;
    import android.speech.tts.TextToSpeech;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;

    import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

  MediaPlayer MP;
  TextToSpeech mTTS;
  boolean readyToTalk = false;

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

    // initialize music

    MP = MediaPlayer.create(getApplicationContext(), R.raw.electronic_clav);
    try {
      MP.prepare();
    } catch (Exception e) { }

    MP.setVolume(.5f, .5f);
    MP.setLooping(true);

    // initialize speech

    mTTS = new TextToSpeech(
        getApplicationContext(),
        new TextToSpeech.OnInitListener() {
          public void onInit(int status) {
            readyToTalk = true;
          }
        });

    //mTTS.setPitch (2.5f);
    mTTS.setSpeechRate(0.5f);

    // can't be mixed into larger string -- and addEarcon doesn't work as expected
    mTTS.addSpeech("(boom)", "cisc181.simplemultimedia", R.raw.grenade);

  }

  // handle music events

  public void handlePlay(View V) {
    MP.start();
  }

  public void handlePause(View V) {
    MP.pause();
  }

  // handle speech events

  public void handleSaySomething(View V) {

    String myTextString;

    EditText ET = (EditText) findViewById(R.id.speech_text_id);

    // if the user has typed something, say it

    if (ET.getText().toString().length() > 0) {
      myTextString = ET.getText().toString();
    }

    // otherwise just use the hint string

    else {
      myTextString = ET.getHint().toString();
    }

    if (readyToTalk) {
      mTTS.speak(
          myTextString,
          TextToSpeech.QUEUE_ADD, // compare to QUEUE_ADD
          null);
    }
  }

}
