title: Android AudioTrack Error
body: <p>I'm currently trying to build a multi-waveform oscillator on Android using AudioTrack but however I try implementing the square wave I get the same fatal error, I cant seem to find any way around it, any help would be useful here's my code:</p>
  
  <pre><code>package com.example.jack.synthesiser;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.ToggleButton;
  
  public class MainActivity extends AppCompatActivity implements       View.OnClickListener {
  
  EditText frequencyInput;
  TextView displayFrequency;
  ToggleButton startStop;
  ToggleButton startStopSquare;
  PlayWave wave = new PlayWave();
  PlaySquare sWave = new PlaySquare();
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initializeView();
  }
  
  private void initializeView () {
  
      frequencyInput = (EditText) findViewById(R.id.editText);
      startStop = (ToggleButton) findViewById(R.id.toggleButton);
      startStopSquare = (ToggleButton) findViewById(R.id.toggleButton2);
      displayFrequency = (TextView) findViewById(R.id.textView);
      startStop.setOnClickListener(this);
  }
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      int frequency = Integer.parseInt(frequencyInput.getText().toString());
      displayFrequency.setText(String.valueOf(frequency));
      wave.setWave(frequency);
      sWave.setSquareWave(frequency);
      boolean on = startStop.isChecked();
      boolean square = startStopSquare.isChecked();
      if (on) {
          wave.start();
      } else if (!on){
          wave.stop();
      }
     //if (square) {
     //    sWave.start();
     //} else if (!square) {
     //    sWave.stop();
     //}
  
  
  
  
  
  }
  }
  </code></pre>
  
  <p>And my Play wave class looks like this:</p>
  
  <pre><code>package com.example.jack.synthesiser;
  
  import android.media.AudioFormat;
  import android.media.AudioManager;
  import android.media.AudioTrack;
  
  /**
    * Created by Jack on 16/01/2017.
    */
  
  public class PlayWave {
  
  private final int SAMPLE_RATE = 44100; //maximum sample rate Audiotrack can use
  private AudioTrack mAudio;
  int buffsize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
  private int sampleCount;
  
  public PlayWave() {
  
      mAudio = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
                              buffsize, AudioTrack.MODE_STATIC);
  }
  
  public void setWave (int frequency) {
  
      sampleCount = (int) ((float) SAMPLE_RATE/frequency);
      short samples[] = new short[sampleCount]; //array to hold samples
      int amplitude = 32767; //max amplitude can reach
      double twoPi = 8. * Math.atan(1.); //twoPi calculated as the arctangent of 1 multiplied by 8
      double phase = 0.0;
  
      for (int i = 0;  i &lt; sampleCount;  i++) {
          samples[i] = (short) (amplitude * Math.sin(phase));
          phase += twoPi * frequency/SAMPLE_RATE;
      }
      mAudio.write(samples, 0, sampleCount);
  }
  
  public void setSquareWave (int frequency) {
      sampleCount = (int) ((float) SAMPLE_RATE/frequency);
      short samples[] = new short[sampleCount];
      int amplitude = 32767;
      double phase = 0.0;
      double twoPi = 8. * Math.atan(1.);
      for (int i = 0; i &lt; sampleCount; i++) {
          samples[i] = (short) (amplitude * Math.sin(phase));
          if (samples[i] &gt; (short) 0.0) {
              samples[i] = 32767;
          }
  
          if (samples[i] &lt; (short) 0.0) {
              samples[i] = -32767;
          }
          phase += twoPi * frequency/SAMPLE_RATE;
      }
  }
  
  public void start () {
      mAudio.reloadStaticData();
      mAudio.setLoopPoints(0, sampleCount, -1);
      mAudio.play();
  }
  
  public void stop () {
      mAudio.stop();
  
  }
  }
  </code></pre>
  
  <p>The error i get is this: </p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: com.example.jack.synthesiser, PID: 18957
                java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
                    at android.media.AudioTrack.play(AudioTrack.java:1141)
                    at com.example.jack.synthesiser.PlayWave.start(PlayWave.java:61)
                    at com.example.jack.synthesiser.MainActivity.onClick(MainActivity.java:47)
                    at android.view.View.performClick(View.java:4780)
                    at android.widget.CompoundButton.performClick(CompoundButton.java:120)
                    at android.view.View$PerformClick.run(View.java:19866)
                    at android.os.Handler.handleCallback(Handler.java:739)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>